1 Symmetric Tree
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.
talk is cheap let me show you code
递归:
class Solution { public boolean isSymmetric(TreeNode root) { return isMirror(root,root); } private boolean isMirror(TreeNode left, TreeNode right) { if (left == null && right == null ) { return true ; } if (left == null || right == null ) { return false ; } return left.val == right.val && isMirror(left.left,right.right) && isMirror(left.right,right.left); } } |
非递归:
public boolean isSymmetric2(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); queue.add(root); while (!queue.isEmpty()) { TreeNode t1 = ((LinkedList<TreeNode>) queue).pop(); TreeNode t2 = ((LinkedList<TreeNode>) queue).pop(); if (t1 == null && t2 == null ) continue ; if (t1 == null || t2 == null ) return false ; if (t1.val != t2.val) return false ; queue.add(t1.left); queue.add(t2.right); queue.add(t1.right); queue.add(t2.left); } return true ; } |
昨天的 same tree
非递归:
public boolean isSameTree(TreeNode p,TreeNode q) { Queue<TreeNode> queue = new LinkedList<>(); queue.add(p); queue.add(q); while (!queue.isEmpty()) { TreeNode t1 = ((LinkedList<TreeNode>) queue).pop(); TreeNode t2 = ((LinkedList<TreeNode>) queue).pop(); if (t1 == null && t2 == null ) continue ; if (t1 == null || t2 == null ) return false ; if (t1.val != t2.val) return false ; queue.add(t1.left); queue.add(t2.left); queue.add(t1.right); queue.add(t2.right); } return true ; } |
104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Note: A leaf is a node with no children.
Example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its depth = 3.
Accepted
408,537
Submissions
707,797
talk is cheap let me show you the code
public int maxDepth(TreeNode root) { if (root == null ) { return 0 ; } int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); return leftDepth > rightDepth ? leftDepth+ 1 : rightDepth+ 1 ; } |
107. 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).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its bottom-up level order traversal as:
[
[15,7],
[9,20],
[3]
]
Accepted
193,422
Submissions
435,269
talk is cheap let me show you the code
public List<List<Integer>> levelOrderBottom(TreeNode root) { Queue<TreeNode> queue = new LinkedList<>(); List<List<Integer>> allList = new ArrayList<>(); queue.add(root); while (!queue.isEmpty()) { List<Integer> levelList = new ArrayList<>(); int queueSize = queue.size(); for ( int i= 0 ; i< queueSize; i++) { TreeNode node = ((LinkedList<TreeNode>) queue).pop(); if (node != null ) { levelList.add(node.val); if (node.left != null ) { queue.add(node.left); } if (node.right != null ) { queue.add(node.right); } } } if (!levelList.isEmpty()) allList.add(levelList); } Collections.reverse(allList); return allList; } |
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted array: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/ \
-3 9
/ /
-10 5
Accepted
215,250
Submissions
452,314
talk is cheap let me show you the code
class Solution { public TreeNode sortedArrayToBST( int [] nums) { if (nums == null || nums.length == 0 ) { return null ; } return bst(nums, 0 ,nums.length- 1 ); } public TreeNode bst( int [] nums, int begin, int end) { if (begin > end ) { return null ; } int middle = begin + (end - begin)/ 2 ; TreeNode treeNode = new TreeNode(nums[middle]); treeNode.left = bst(nums,begin,middle- 1 ); treeNode.right = bst(nums,middle+ 1 ,end); return treeNode; } } |
110. Balanced Binary Tree
For this problem, a height-balanced binary tree is defined as:
a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example 1:
Given the following tree [3,9,20,null,null,15,7]
:
3
/ \
9 20
/ \
15 7
Return true.
Example 2:
Given the following tree [1,2,2,3,3,null,null,4,4]
:
1
/ \
2 2
/ \
3 3
/ \
4 4
Return false.
Accepted
270,541
Submissions
682,299
talk is cheap show you me the code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isBalanced(TreeNode root) { if (root == null ) { return true ; } int leftDepth = depthTree(root.left); int rightDepth = depthTree(root.right); return Math.abs(leftDepth - rightDepth) <= 1 && isBalanced(root.left) && isBalanced(root.right); } public int depthTree (TreeNode root) { if (root == null ) { return 0 ; } int leftDepth = depthTree(root.left); int rightDepth = depthTree(root.right); return leftDepth > rightDepth ? leftDepth+ 1 : rightDepth+ 1 ; } } |
ps:
什么才是幸福呢
有的人拥有的很少,却看似很多
有的人明明拥有很多,却看似很少
反而拥有很少的那个却很幸福,后者却不幸福
为什么呢,是拥有多的不知足,还是拥有少的太容易满足?
昨天听妈妈说 一很亲的邻居去打工,爬很高的楼被砸死了,听了以后心情异常的沉重,小时候常去他家买东西,很热情,而且与父亲的关系很好,我心情沉重了好久,什么才是幸福,可能活的长久,没病没灾
吃饱喝足就是很大的幸福吧,没了生命,不能活着,何谈好好生活呢,珍惜自己所拥有的,快快乐乐的,看似拥有的很好,但是满满的幸福在心里,能说拥有的少吗