二叉树逐层打印
1 | class Node: |
1 | a=Node(1,Node(2),Node(3)) |
1
1 | printNode(a) |
[[1], [2, 3]]
二叉树前序遍历(非递归)
1 | def print_node(root): |
1
2
4
5
3
6
7
二叉树中序遍历 (非递归)
1 | def print_node(root): |
4
2
5
1
6
3
7
二叉树后序遍历 (非递归)
1 | def print_node(root): |
4
5
2
6
7
3
1
110. balance binary tree
1 | def check(root): |
False
100. same tree
1 | def same_tree(p,q): |
False
True
101. symmetric tree
1 | def symmetric_tree(root): |
True
False
116. Populating Next Right Pointers in Each Node
1 | def populate_next_right_pointer(root): |
102. Binary Tree Level Order Traversal
1 | def binary_tree_level_order_traversal(root): |
[[1], [2, 2], [3, 4, 4, 3]]
107. Binary Tree Level Order Traversal II
1 | def binary_tree_level_order_traversal(root): |
[[3, 4, 4, 3], [2, 2], [1]]
103. Binary Tree Zigzag Level Order Traversal
1 | def binary_tree_level_order_traversal(root): |
[[3], [20, 9], [15, 7]]
637. Average of Levels in Binary Tree
1 | def binary_tree_level_order_traversal(root): |
[3.0, 14.5, 11.0]
314. Binary Tree Vertical Order Traversal
1 | from collections import defaultdict |
[[9], [3, 15], [20], [7]]
257. Binary Tree Paths
1 | def binary_tree_paths(root): |
[[3, 1], [3, 2]]
112. Path Sum
1 | def path_sum(root,n): |
True
113. Path Sum II
1 | def path_sum(root,n): |
[[3, 2]]
129. Sum Root to Leaf Numbers
1 | def path_sum(root,n): |
63
124. Binary Tree Maximum Path Sum
1 | def binary_tree_maximum_path_sum(root): |
42
563. Binary Tree Tilt
1 | def add(root): |
235. Lowest Common Ancestor of a Binary Search Tree
1 | def lowest_common_ancestor(root,p,q): |
270. Closest Binary Search Tree Value
1 | def closest_binary_search_tree(root,target): |
3
4
272. Closest Binary Search Tree Value II
这道题要维持一个k长度的list,所以可以中序遍历,然后不断更新最后添加元素和队列首元素与target的差值
1 | class Solution(object): |
1