257 binary tree paths
https://github.com/tongzhang1994/Facebook-Interview-Coding/blob/master/257.%20Binary%20Tree%20Paths.java
Given a binary tree, return all root-to-leaf paths.
Note: A leaf is a node with no children.
Example:
Input: 1 / \ 2 3 \ 5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3
// dfs recursive
class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> answer = new ArrayList<String>(); if(root != null){ dfs(root, "", answer); } return answer; } private void dfs(TreeNode root, String path, List<String> answer){ if(root.left == null && root.right == null){ answer.add(path + root.val); } if(root.left != null){ dfs(root.left, path + root.val + "->", answer); } if(root.right != null){ dfs(root.right, path + root.val + "->", answer); } } } class Solution { public List<String> binaryTreePaths(TreeNode root) { // iterative dfs preorder // use two stacks // one for tree node // another one fis for stringbuilder List<String> res = new ArrayList<>(); if(root == null) return res; Stack<TreeNode> stack = new Stack<>(); Stack<StringBuilder> paths = new Stack<>(); stack.push(root); paths.push(new StringBuilder(root.val + "")); // why ""? to make it as a string, otherwise, its just an int while(!stack.isEmpty()){ TreeNode node = stack.pop(); StringBuilder path = paths.pop(); if(node.left == null && node.right == null){ res.add(path.toString()); } if(node.right != null){ stack.push(node.right); StringBuilder newPath = new StringBuilder(path); paths.push(newPath.append("->").append(node.right.val)); } if(node.left != null){ stack.push(node.left); StringBuilder newPath = new StringBuilder(path); paths.push(newPath.append("->").append(node.left.val)); } } return res; } } // if we can't use dfs, then use bfs // use hashmap to store parent to children paths class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> res = new ArrayList<>(); if(root == null) return res; HashMap<TreeNode, TreeNode> map = new HashMap<>(); Queue<TreeNode> queue = new LinkedList<>(); queue.offer(root); map.put(root, null); // key is node, value is node's parent while(!queue.isEmpty()){ TreeNode cur = queue.poll(); if(cur.left == null && cur.right == null){ String path = getPath(map, cur); res.add(path); } if(cur.left != null){ map.put(cur.left, cur); queue.offer(cur.left); } if(cur.right != null){ map.put(cur.right, cur); queue.offer(cur.right); } } return res; } private String getPath(HashMap<TreeNode, TreeNode> map, TreeNode node){ StringBuilder sb = new StringBuilder(); while(node != null){ String nodeVal = String.valueOf(node.val); StringBuilder val = new StringBuilder(nodeVal).reverse(); sb.append(val + ">-"); node = map.get(node); } return sb.reverse().substring(2); } } // iteraive bfs , use queue to store ArrayList<TreeNode>, which is the path class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> res = new ArrayList<>(); if(root == null) return res; Queue<ArrayList<TreeNode>> queue = new LinkedList<>(); queue.offer(new ArrayList<>(Arrays.asList(root))); while(!queue.isEmpty()){ ArrayList<TreeNode> path = queue.poll(); TreeNode cur = path.get(path.size() - 1); if(cur.left == null && cur.right == null){ StringBuilder sb = new StringBuilder(); // construct the path from the arraylist of tree node for(TreeNode node : path){ sb.append(node.val + "->"); } // subtract the last -> part sb.setLength(sb.length() - 2); res.add(sb.toString()); continue; } if(cur.left != null){ ArrayList<TreeNode> newPath = new ArrayList<>(path); newPath.add(cur.left); queue.offer(newPath); } if(cur.right != null){ ArrayList<TreeNode> newPath = new ArrayList<>(path); newPath.add(cur.right); queue.offer(newPath); } } return res; } }
posted on 2018-08-11 04:38 猪猪🐷 阅读(104) 评论(0) 收藏 举报
浙公网安备 33010602011771号