✅One Stop for Trees solution ✔️✔️|| Iterative || Recursion|| DFS BSF
Recommended to know FULLY all these approaches.\n\nMain Pattern  --> Right Side view of A tree\n\n\n\n# BFS\n# Code\n\nimport java.util.*;\n\npublic class Solut
#### Recommended to know **FULLY** all these approaches.\n\n**Main Pattern**  --> [Right Side view of A tree](https://leetcode.com/problems/binary-tree-right-side-view/solutions/5289601/one-stop-for-trees-solution-iterative-recursion-dfs-bsf/)\n\n\n\n# BFS\n# Code\n```\nimport java.util.*;\n\npublic class Solution {\n    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {\n        // Map to mark the parents of all nodes\n        HashMap<TreeNode, TreeNode> parentTrack = new HashMap<>();\n\n        \n        markParents(root, parentTrack); // Mark parents of all nodes\n        \n        HashMap<TreeNode, Boolean> visited = new HashMap<>(); // Keeps track of visited nodes\n        \n        List<Integer> result = new ArrayList<>(); // List to store nodes at distance K from the target\n        \n        Queue<TreeNode> queue = new LinkedList<>(); // Queue to perform level-order traversal\n\n\n        \n        queue.add(target); // Start traversal from the TARGET node\n        visited.put(target, true);\n        \n        int currLevel = 0;  // Tracks the current level while traversing the tree\n\n        // Continue traversal until the queue is empty\n        while (!queue.isEmpty()) {\n            // Get the number of nodes at the current level\n            int size = queue.size();\n            if (currLevel == k) {\n                // Break if the current level matches the required distance (k)\n                while (!queue.isEmpty()) {\n                    // Extract nodes at distance k from the queue\n                    TreeNode current = queue.poll();\n                    // Store node values in the result list\n                    result.add(current.val);\n                }\n                break;\n            }\n\n            // Traverse the current level of the tree\n            for (int i = 0; i < size; i++) {\n                // Get the front node in the queue\n                TreeNode current = queue.poll();\n\n                // Add unvisited left child to the queue\n                if (current.left != null && !visited.containsKey(current.left)) {\n                    queue.add(current.left);\n                    // Mark left child as visited\n                    visited.put(current.left, true);\n                }\n\n                // Add unvisited right child to the queue\n                if (current.right != null && !visited.containsKey(current.right)) {\n                    queue.add(current.right);\n                    // Mark right child as visited\n                    visited.put(current.right, true);\n                }\n\n                // Add unvisited parent node to the queue\n                if (parentTrack.containsKey(current) &&\n                        !visited.containsKey(parentTrack.get(current))) {\n                    queue.add(parentTrack.get(current));\n                    // Mark parent node as visited\n                    visited.put(parentTrack.get(current), true);\n                }\n            }\n            currLevel++;\n        }\n\n        // Return nodes at distance K from the target\n        return result;\n    }\n\n    // Helper function to mark parents of nodes in the tree\n    void markParents(TreeNode root, HashMap<TreeNode, TreeNode> parentTrack) {\n        // Level Order Traversal by taking a queue\n        Queue<TreeNode> queue = new LinkedList<>();\n        queue.add(root);\n\n        // Iterate over all nodes\n        while (!queue.isEmpty()) {\n            TreeNode current = queue.poll();\n\n            // Assign parents to left child and right child if they exist\n            if (current.left != null) {\n                parentTrack.put(current.left, current);\n                queue.add(current.left);\n            }\n\n            if (current.right != null) {\n                parentTrack.put(current.right, current);\n                queue.add(current.right);\n            }\n        }\n    }\n}\n\n```\n\n# DFS\n\n# Code\n```\nclass Solution {\n    \n    Map<TreeNode, Integer> map = new HashMap<>();\n        \n    public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {\n        List<Integer> res = new LinkedList<>();\n        find(root, target);\n        dfs(root, target, K, map.get(root), res);\n        return res;\n    }\n    \n    // find target node first and store the distance in that path that we could use it later directly\n    private int find(TreeNode root, TreeNode target) {\n        if (root == null) return -1;\n        if (root == target) {\n            map.put(root, 0);\n            return 0;\n        }\n        int left = find(root.left, target);\n        if (left >= 0) {\n            map.put(root, left + 1);\n            return left + 1;\n        }\n\t\tint right = find(root.right, target);\n\t\tif (right >= 0) {\n            map.put(root, right + 1);\n            return right + 1;\n        }\n        return -1;\n    }\n    \n    private void dfs(TreeNode root, TreeNode target, int K, int length, List<Integer> res) {\n        if (root == null) return;\n        if (map.containsKey(root)) length = map.get(root);\n        if (length == K) res.add(root.val);\n        dfs(root.left, target, K, length + 1, res);\n        dfs(root.right, target, K, length + 1, res);\n    }\n}\n```\n[reference link](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/solutions/143798/1ms-beat-100-simple-java-dfs-with-without-hashmap-including-explanation/)\n\n# code optimized and interview recommended\n\n```\nclass Solution {\n            \n    public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {\n        List<Integer> res = new LinkedList<>();\n        if (K == 0) {\n            res.add(target.val);\n        } else {\n            dfs(res, root, target.val, K ,0);\n        }\n        return res;\n    }\n    \n    private int dfs(List<Integer> res, TreeNode node, int target, int K, int depth) {\n        if (node == null) return 0;\n        \n        if (depth == K) {\n            res.add(node.val);\n            return 0;\n        }\n        \n        int left, right;\n        if (node.val == target || depth > 0) {\n            left = dfs(res, node.left, target, K, depth + 1);\n            right = dfs(res, node.right, target, K, depth + 1);\n        } else {\n            left = dfs(res, node.left, target, K, depth);\n            right = dfs(res, node.right, target, K, depth);\n        }\n        \n        if (node.val == target) return 1;\n        \n        if (left == K || right == K) {\n            res.add(node.val);\n            return 0;\n        }\n        \n        if (left > 0) {\n            dfs(res, node.right, target, K, left + 1);\n            return left + 1;\n        }\n        \n        if (right > 0) {\n            dfs(res, node.left, target, K, right + 1);\n            return right + 1;\n        }\n        \n        return 0;\n    }\n}\n```\n\n[Children Sum Property](https://www.naukri.com/code360/problems/children-sum-property_8357239?utm_source=striver&utm_medium=website&utm_campaign=a_zcoursetuf&leftPanelTabValue=PROBLEM)\n```java []\n\n//DFS NLR preorder\npublic class Solution {\n\n    public static boolean isParentSum(Node root) {\n        return reorder(root);\n    }\n\n    public static boolean reorder(Node root) {\n        // Write your code here.\n        if (root == null) return true;\n\n        int child = 0;\n        if (root.left != null) {\n            child += root.left.data;\n        }\n        if (root.right != null) {\n            child += root.right.data;\n        }\n\n        if ((root.left != null || root.right != null) && child != root.data) {\n            return false;\n        }\n\n        boolean leftResult = reorder(root.left);\n        boolean rightResult = reorder(root.right);\n\n        return leftResult && rightResult;\n    }\n}\n\n```\n```java []\n//BFS iterative approach\nimport java.util.LinkedList;\nimport java.util.Queue;\n\npublic class Solution {\n\n    public static boolean isParentSum(Node root) {\n        \n\n        if(root==null) return true;\n        Queue<Node> q = new LinkedList<>();\n        q.offer(root);\n\n        while(!q.isEmpty()){\n            int sum =0;\n            Node cur = q.poll();\n\n            if(cur.left!=null){\n                sum+=cur.left.data;\n                q.offer(cur.left);\n            }\n            if(cur.right!=null){\n                sum+=cur.right.data;\n                q.offer(cur.right);\n            }\n            if(sum==0 && cur.left==null && cur.right==null) continue;\n            if(sum!=cur.data) return false;\n        }\n        return true;\n    }\n}\n\n```\n# Time To Burn Tree\nProblem statement\nYou have a binary tree of \'N\' unique nodes and a Start node from where the tree will start to burn. Given that the Start node will always exist in the tree, your task is to print the time (in minutes) that it will take to burn the whole tree.\n\n\n\nIt is given that it takes 1 minute for the fire to travel from the burning node to its adjacent node and burn down the adjacent node.\n\n\n\nFor Example :\nFor the given binary tree: [1, 2, 3, -1, -1, 4, 5, -1, -1, -1, -1]\nStart Node: 3\n\n    1\n   / \\\n  2   3\n     / \\\n    4   5\n\nOutput: 2\n\nExplanation :\nIn the zeroth minute, Node 3 will start to burn.\n\nAfter one minute, Nodes (1, 4, 5) that are adjacent to 3 will burn completely.\n\nAfter two minutes, the only remaining Node 2 will be burnt and there will be no nodes remaining in the binary tree. \n\nSo, the whole tree will burn in 2 minutes.\n\n# Code\n```\nimport java.util.*;\npublic class Solution\n{\npublic static int timeToBurnTree(TreeNode root, int start)\n    {\n       \n        if(root ==null) return 0;\n\t\t\n\n        HashMap<TreeNode,TreeNode> mark = new HashMap<>();\n        TreeNode tt =markParent(root,mark,start);\n\n        HashMap<TreeNode,Boolean> visited = new HashMap<>();\n\n        Queue<TreeNode> q = new LinkedList<>();\n        q.offer(tt);\n        visited.put(tt,true);\n        int currentLevel =0;\n\n        while(!q.isEmpty()){\n\n            int size = q.size();\n\n            for(int i=0;i<size;i++){\n                \n                TreeNode cur =q.poll();\n\n                if(cur.left!=null && !visited.containsKey(cur.left)){\n                    q.offer(cur.left);\n                    visited.put(cur.left,true);\n                }\n                if(cur.right!=null && !visited.containsKey(cur.right)){\n                    q.offer(cur.right);\n                    visited.put(cur.right,true);\n                }\n                if(mark.containsKey(cur)&& !visited.containsKey(mark.get(cur))){\n                    q.offer(mark.get(cur));\n                    visited.put(mark.get(cur),true);\n                }\n            }\n            currentLevel++;\n        }\n        return currentLevel-1;\n    }\n    static TreeNode markParent(TreeNode root,HashMap<TreeNode,TreeNode> mark, int start ){\n\n        Queue<TreeNode> q = new LinkedList<>();\n        q.offer(root);\n\t\tTreeNode res = new TreeNode(-1);\n        while(!q.isEmpty()){\n            TreeNode cur = q.poll();\n\t\t\tif(cur.data == start) res= cur;\n            if(cur.left!=null){\n                q.offer(cur.left);\n                mark.put(cur.left,cur);\n            }\n            if(cur.right!=null){\n                q.offer(cur.right);\n                mark.put(cur.right,cur);\n            }\n        }\n\t\treturn res;\n    }\n}\n```
['Hash Table', 'Depth-First Search', 'Breadth-First Search', 'Binary Tree', 'Java']