101. Symmetric Tree
问题描述:
- Total Accepted: 145790
- Total Submissions: 397211
- Difficulty: Easy
- Contributors: Admin
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
解析:
判断一个二叉树是否是镜像的。使用栈处理数据,注意左右节点的入栈顺序。
public class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
Stack<TreeNode> stack=new Stack<TreeNode>();
stack.push(root.left);
stack.push(root.right);
while(!stack.isEmpty()){
TreeNode node1=stack.pop();
TreeNode node2=stack.pop();
if(node1==null && node2==null)
continue;
if(node1==null || node2==null || node1.val!=node2.val){
return false;
}
stack.push(node1.left);
stack.push(node2.right);
stack.push(node1.right);
stack.push(node2.left);
}
return true;
}
}
102. Binary Tree Level Order Traversal
问题描述:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
解析:
层序遍历二叉树,每一层节点的值分别放在一个list中,所有list放在一个大list中。
思想:
* 深度优先搜索,按层遍历,每层的元素分别放在一个list中,最后将多有list汇总到一个大list中。
* 核心,使用队列;
* 第一层先入队列,在遍历第一层元素的同时,将第二层入队列,第一层并出队列,第一层出去完之后,只剩下第二层数据了。
* 第二层开始遍历,第二层遍历的过程中,将第三层入队列。。。依次类推,直至最后一层入队列,遍历完最后一层,队列就空了。循环结束。
public static List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<TreeNode>();
List<List<Integer>> list=new LinkedList<List<Integer>>();
if(root==null)
return list;
queue.offer(root);
while(!queue.isEmpty()){
int len=queue.size();
List<Integer> subList=new LinkedList<Integer>();
for(int i=0;i<len;i++){
if(queue.peek().left!=null) queue.offer(queue.peek().left);
if(queue.peek().right!=null) queue.offer(queue.peek().right);
subList.add(queue.poll().val);
}
list.add(subList);
}
return list;
}
110. Balanced Binary Tree
问题描述:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never differ by more than 1.
解析:
public static boolean isBalanced(TreeNode root) {
if(root==null)
return true;
if(Math.abs(height(root.left)-height(root.right))<=1){
return (isBalanced(root.left)&&(isBalanced(root.right)));
}
return false;
}
private static int height(TreeNode root) {
// TODO Auto-generated method stub
if(root==null){
return 0;
}
int left=height(root.left);
int right=height(root.right);
return (Math.max(left, right)+1);
}
111. Minimum Depth of Binary Tree
问题描述:
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.
解析:
public int minDepth(TreeNode root) {
if(root==null)
return 0;
if(root.left==null)
return minDepth(root.right)+1;
if(root.right==null)
return minDepth(root.left)+1;
return Math.min(minDepth(root.left), minDepth(root.right))+1;
}
112. Path Sum
问题描述:
-
Total Accepted: 136461
-
Total Submissions: 415931
-
Difficulty: Easy
-
Contributors: Admin
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.
For example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path
5->4->11->2
which sum is 22.
解析:
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null)
return false;
if(root.left==null && root.right==null && sum-root.val==0)
return true;
return hasPathSum(root.left,sum-root.val) || hasPathSum(root.right,sum-root.val);
}
257. Binary Tree Paths
问题描述:
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
解析:
找到二叉树中根节点到叶子节点的所有路径。
public List<String> binaryTreePaths(TreeNode root) {
List<String> list=new ArrayList<String>();
if(root!=null) searchBT(root,"",list);
return list;
}
private static void searchBT(TreeNode root, String path, List<String> list) {
// TODO Auto-generated method stub
if(root.left==null && root.right==null)
list.add(path+root.val);
if(root.left!=null)
searchBT(root.left, path+root.val+"->", list);
if(root.right!=null)
searchBT(root.right, path+root.val+"->", list);
}