144. 二叉樹的前序遍歷:
鏈接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/
給定一個二叉樹,返回它的 前序 遍歷。
示例:
輸入: [1,null,2,3]
1
\
2
/
3
輸出: [1,2,3]
遞歸算法:
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root!=null){
res.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
}
return res;
}
}
94. 二叉樹的中序遍歷:
鏈接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
給定一個二叉樹,返回它的中序 遍歷。
示例:
輸入: [1,null,2,3]
1
\
2
/
3
輸出: [1,3,2]
遞歸算法:
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root!=null){
inorderTraversal(root.left);
res.add(root.val);
inorderTraversal(root.right);
}
return res;
}
}
145. 二叉樹的後序遍歷:
鏈接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
給定一個二叉樹,返回它的 後序 遍歷。
示例:
輸入: [1,null,2,3]
1
\
2
/
3
輸出: [3,2,1]
遞歸算法:
class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root!=null){
postorderTraversal(root.left);
postorderTraversal(root.right);
res.add(root.val);
}
return res;
}
}
100. 相同的樹:
鏈接:https://leetcode-cn.com/problems/same-tree/
給定兩個二叉樹,編寫一個函數來檢驗它們是否相同。
如果兩個樹在結構上相同,並且節點具有相同的值,則認爲它們是相同的。
示例 1:
輸入: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
輸出: true
代碼:
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q== null){
return true;
}
if(p==null || q==null){
return false;
}
if(p.val!=q.val){//根不相同
return false;
}
return
isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
572. 另一個樹的子樹:
鏈接: https://leetcode-cn.com/problems/subtree-of-another-tree/
給定兩個非空二叉樹 s 和 t,檢驗 s 中是否包含和 t 具有相同結構和節點值的子樹。s 的一個子樹包括 s 的一個節點和這個節點的所有子孫。s 也可以看做它自身的一棵子樹。
示例 1:
給定的樹 s:
3
/ \
4 5
/ \
1 2
給定的樹 t:
4
/ \
1 2
返回 true,因爲 t 與 s 的一個子樹擁有相同的結構和節點值。
示例 2:
給定的樹 s:
3
/ \
4 5
/ \
1 2
/
0
給定的樹 t:
4
/ \
1 2
返回 false。
代碼:
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s==null)
{
return false;
}
if(t==null){ //子樹爲空
return true;
}
if(s.val==t.val && isSameTree(s,t)){
return true;//兩棵樹相同
}
return isSubtree(s.left,t)||isSubtree(s.right,t);
}
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null && q== null){
return true;
}
if(p==null || q==null){
return false;
}
if(p.val!=q.val){
return false;
}
return
isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
104. 二叉樹的最大深度:
鏈接: https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
給定一個二叉樹,找出其最大深度。
二叉樹的深度爲根節點到最遠葉子節點的最長路徑上的節點數。
說明: 葉子節點是指沒有子節點的節點。
示例:
給定二叉樹 [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回它的最大深度 3 。
代碼:
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int a = maxDepth(root.left);
int b = maxDepth(root.right);
return Math.max(a,b) + 1;
}
}
110. 平衡二叉樹:
鏈接: https://leetcode-cn.com/problems/balanced-binary-tree/
示例 1:
給定二叉樹 [3,9,20,null,null,15,7]
3
/ \
9 20
/ \
15 7
返回 true 。
示例 2:
給定二叉樹 [1,2,2,3,3,null,null,4,4]
1
/ \
2 2
/ \
3 3
/ \
4 4
返回 false 。
代碼:
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null){
return true;
}
int a = maxDepth(root.left);
int b = maxDepth(root.right);
if(Math.abs(a-b)>1){
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
}
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
}
}