从零开始Leetcode – Day 9
##Binary Tree Zigzag Level Order Traversal
这题和Binary Tree Level Order Traversal 很想。 要注意count是从第二层开始算。
Binary Tree Zigzag Level Order Traversal
广度优先的WIKI
https://en.wikipedia.org/wiki/Breadth-first_search
明早再练习这两题。
1 |
|
##Binary Tree Zigzag Level Order Traversal
这题和Binary Tree Level Order Traversal 很想。 要注意count是从第二层开始算。
Binary Tree Zigzag Level Order Traversal
广度优先的WIKI
https://en.wikipedia.org/wiki/Breadth-first_search
明早再练习这两题。
1 |
|
https://leetcode.com/problems/sum-root-to-leaf-numbers/
用private function 做, 这样子sum就在function里了。 leetcode OJ 不能全局变量。
1 |
|
https://leetcode.com/problems/sum-root-to-leaf-numbers/
把sum变成了array(object), 然后再pass by reference. primitive type 是pass by value.
1 |
|
进入medium 的tree了, 这个有bug, 明天继续
https://leetcode.com/problems/path-sum-ii/
要记得每次递归过后,要把上一次的pop()出来。
1 |
|
5,6,7被吞掉了。
不会广度优先搜索, Binary Tree Level Order Traversal 这题不会做。
https://leetcode.com/problems/binary-tree-level-order-traversal/
明天加油。
https://leetcode.com/problems/binary-tree-level-order-traversal/1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrder = function(root) {
var res = [];
if(!root){
return res;
}
var queue = [];
queue.push(root);
var curNum = 0;
var lastNum = 1;
var list = [];
while(queue.length){
var cur = queue.shift();
lastNum --;
list.push(cur.val);
if(cur.left !== null){
queue.push(cur.left);
curNum++;
}
if(cur.right !== null){
queue.push(cur.right);
curNum ++;
}
if(lastNum === 0){
lastNum = curNum;
curNum = 0;
res.push(list);
list = [];
}
}
return res;
};
LeetCode总结 – 树的性质篇
http://blog.csdn.net/linhuanmars/article/details/39024195
这个博客分析得不错。
总结一下今天:
Given a binary tree, return all root-to-leaf paths.
https://leetcode.com/problems/binary-tree-paths/
1 | var binaryTreePaths = function(root) { |
这题卡了半天。 这个[“1->2->5”, “1->3”] 到底要怎么实现。 今天挂了。
◢▆▅▄▃崩╰(〒皿〒)╯潰▃▄▅▇◣ 明天还要早起, GG。
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
1 | var lowestCommonAncestor = function(root, p, q) { |
1 | var invertTree = function(root) { |
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
1 | var minDepth = function(root) { |
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
1 | var hasPathSum = function(root, sum) { |
1 | var isSymmetric = function(root) { |
1 | var isSameTree = function(p, q) { |
1 |
|
1 |
|
##Binary Tree Level Order Traversal II
https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
class Solution:
# @param root, a tree node
# @return a list of lists of integers
def levelOrderBottom(self, root):
if not root:
return[]
result = []
self.helper(root, 0, result)
result.reverse()
return result
def helper(self, root, level, result):
if not root:
return
if level+1 > len(result):
result.append([])
result[level].append(root.val)
self.helper(root.left, level+1, result)
self.helper(root.right, level+1, result)