輸入兩棵二叉樹A和B,判斷B是不是A的子結構。(約定空樹不是任意一個樹的子結構)
B是A的子結構, 即 A中有出現和B相同的結構和節點值。
例如:
給定的樹 A:
?????3
????/ \
???4 ??5
??/ \
?1 ??2
給定的樹 B:
???4?
??/
?1
返回 true,因為 B 與 A 的一個子樹擁有相同的結構和節點值。
示例 1:
輸入:A = [1,2,3], B = [3,1]
輸出:false
示例 2:
輸入:A = [3,4,5,1,2], B = [4,1]
輸出:true
思路:遍歷樹(前中后都可以,不影響),對每個結點判斷是否是子樹。
唯一要注意的是定義:
就算圖中的1還有左右孩子,依舊算匹配成功。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
class Solution {public boolean isSubStructure(TreeNode A, TreeNode B) {if(A == null || B == null){return false;}return issub(A,B) || isSubStructure(A.left,B) || isSubStructure(A.right,B);}//判斷是否是子樹public boolean issub(TreeNode A, TreeNode B){if(B == null){return true;}if(A == null && B != null){return false;}if(A.val == B.val){return issub(A.left,B.left) && issub(A.right,B.right);}return false;}
}
請完成一個函數,輸入一個二叉樹,該函數輸出它的鏡像。
例如輸入:
?????4
???/ ??\
??2 ????7
?/ \ ??/ \
1 ??3 6 ??9
鏡像輸出:
?????4
???/ ??\
??7 ????2
?/ \ ??/ \
9 ??6 3???1
?
示例 1:
輸入:root = [4,2,7,1,3,6,9]
輸出:[4,7,2,9,6,3,1]
思路,找遞歸定義。
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
class Solution {public TreeNode mirrorTree(TreeNode root) {if(root==null){return null;}TreeNode temp=root.left;root.left=root.right;root.right=temp;mirrorTree(root.left);mirrorTree(root.right);return root;}
}
請實現一個函數,用來判斷一棵二叉樹是不是對稱的。如果一棵二叉樹和它的鏡像一樣,那么它是對稱的。
例如,二叉樹?[1,2,2,3,4,4,3] 是對稱的。
????1
???/ \
??2 ??2
?/ \ / \
3 ?4 4 ?3
但是下面這個?[1,2,2,null,3,null,3] 則不是鏡像對稱的:
????1
???/ \
??2 ??2
???\ ??\
???3 ???3
?
示例 1:
輸入:root = [1,2,2,3,4,4,3]
輸出:true
示例 2:
輸入:root = [1,2,2,null,3,null,3]
輸出:false
思路:遞歸判斷
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
class Solution {public boolean isSymmetric(TreeNode root) {if(root==null){return true;}return help(root.left,root.right);}public boolean help(TreeNode node1,TreeNode node2){if(node1==null && node2==null){return true;}if(node1==null || node2==null){return false;}return node1.val==node2.val && help(node1.left,node2.right) && help(node1.right,node2.left);}
}
輸入一個矩陣,按照從外向里以順時針的順序依次打印出每一個數字。
示例 1:
輸入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
輸出:[1,2,3,6,9,8,7,4,5]
示例 2:
輸入:matrix =?[[1,2,3,4],[5,6,7,8],[9,10,11,12]]
輸出:[1,2,3,4,8,12,11,10,9,5,6,7]
?
限制:
0 <= matrix.length <= 100
0 <= matrix[i].length?<= 100
思路:一圈一圈往里打印。順時針
class Solution {public int[] spiralOrder(int[][] matrix) {if(matrix == null || matrix.length == 0)return new int[0];int m = matrix.length;int n = matrix[0].length;int[] ans=new int[m*n];int ansIndex=0;int i = 0; //統計矩陣從外向內的層數,如果矩陣非空,那么它的層數至少為1層int count = (Math.min(m, n)+1)/2;//從外部向內部遍歷,逐層打印數據while(i < count) {for (int j = i; j < n-i; j++)ans[ansIndex++]=matrix[i][j];//向右的那一行for (int j = i+1; j < m-i; j++)ans[ansIndex++]=matrix[j][(n-1)-i];//向下的那一列for (int j = (n-1)-(i+1); j >= i && (m-1-i != i); j--)ans[ansIndex++]=matrix[(m-1)-i][j];//向左的那一行for (int j = (m-1)-(i+1); j >= i+1 && (n-1-i) != i; j--)ans[ansIndex++]=matrix[j][i];//向上的那一列i++;} return ans;}
}
定義棧的數據結構,請在該類型中實現一個能夠得到棧的最小元素的 min 函數在該棧中,調用 min、push 及 pop 的時間復雜度都是 O(1)。
?
示例:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); ? --> 返回 -3.
minStack.pop();
minStack.top(); ? ? ?--> 返回 0.
minStack.min(); ? --> 返回 -2.
?
提示:
各函數的調用總次數不超過 20000 次
思路:拿另外一個棧同步記錄當前最即可。
class MinStack {private Stack<Integer> s1;private Stack<Integer> s2;/** initialize your data structure here. */public MinStack() {s1=new Stack<>();s2=new Stack<>();}public void push(int x) {s1.add(x);if(s2.empty()||s2.peek()>x)s2.add(x);else s2.add(s2.peek());}public void pop() {s1.pop();s2.pop();}public int top() {return s1.peek();}public int min() {return s2.peek();}
}
/*** Your MinStack object will be instantiated and called as such:* MinStack obj = new MinStack();* obj.push(x);* obj.pop();* int param_3 = obj.top();* int param_4 = obj.min();*/
?