1st round, 314 binaryTreeVerticalTraversal
来源:互联网 发布:java jlabel 图片 编辑:程序博客网 时间:2024/06/01 09:29
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<List<Integer>> verticalOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<>(); if(root==null) return result; Queue<TreeNode> nodeQ = new LinkedList<>(); // have a clear idea of what nodeQ contains. Queue<Integer> levelQ = new LinkedList<>(); // have a clear idea of what levelQ contains. Map<Integer, List<Integer>> map = new HashMap<>(); // have a clear idea of what map contains. nodeQ.add(root); levelQ.add(0); int min=0; int max=0; while(!nodeQ.isEmpty()){ // while(nodeQ!=null) is wrong!! should check whether it is empty. TreeNode node=nodeQ.poll(); int level=levelQ.poll(); if(!map.containsKey(level)) map.put(level, new ArrayList<Integer>()); map.get(level).add(node.val); // have a clear idea of what map contains. if(node.left!=null){ nodeQ.add(node.left); levelQ.add(level-1); min=Math.min(min, level-1); } if(node.right!=null){ nodeQ.add(node.right); levelQ.add(level+1); max=Math.max(max, level+1); } // for the above two if() statements only if we have some in either left or right child, we add the child to the Q, otherwise need to do nothing. } for(int i=min; i<=max; i++){ result.add(map.get(i)); } return result; }} // have no idea about the question in the beginning, but after looking through solutions I immediately get the idea, it is the BFS approach. Did I know BFS?? Of course!!! You did many quesitons on BFS, basically it is reading elems on same level of tree, and then move to the next level. // here there are many things for me to 总结, such as using Queue<> (Interface) and LinkedList<>(class) to track the information on each level. ALSO, understanding this question and transfering it to some simple questions and easy solutions is a good practice. Lastly, knowing using min, max to keep record of the range is the key point to this question!! 一会好好总结在ppt上。。
这道题,虽然代码略多,但是思路还是非常清晰简练的。。。相信读者看代码就能知道我什么意思。。
0 0
- 1st round, 314 binaryTreeVerticalTraversal
- 1st round, 283 moveZeroes
- 1st round, 325 maxSubArrayLen
- 1st round, 78 Subsets
- 1st round, 1 2Sum
- 1st round, 311 Sparse Matrix Multiplication
- 1st round, 67 Add Binary
- 1st round: 278 First Bad Version
- 1st round, 253 Meeting Rooms II
- 1st round, 91 Decode Ways
- 1st round, 15 3Sum
- 1st round, 277 Find the Celebrity
- 1st round, 200 number of isalnds
- 1st round, 161 One Edit Distance
- 1st round, 257 Binary Tree Paths
- 1st round, 494 Target Sum
- CSAPP 1st round Done & Src to start
- 1st round, 17 Letter Combinations of a Phone Number
- 116. Populating Next Right Pointers in Each Node
- 117. Populating Next Right Pointers in Each Node II
- 118. Pascal's Triangle
- 119. Pascal's Triangle II
- 120. Triangle
- 1st round, 314 binaryTreeVerticalTraversal
- GCC与G++的区别
- FILE文件流的中fopen、fread、fseek、fclose的使用
- Leetcode 203. Remove Linked List Elements
- Leetcode 204. Count Primes
- 策略模式
- 令人拍案叫绝的Wasserstein GAN
- 367. Valid Perfect Square
- 一点 GAN