递归方法解决
public class preorder {
List list=new ArrayList<>();
public List preorder(TreeNode root){
if(root!=null){
list.add(root.val);
preorder(root.left);
preorder(root.right);
}
return list;
}
}
非递归方法解决:利用栈
public List preorderTraversal(TreeNode root){
List list=new ArrayList<>();
Stack stack=new Stack<>();
if(root==null){
return list;
}
stack.push(root);
while(!stack.isEmpty()){
root=stack.pop();
list.add(root.val);
if(root.right!=null){
stack.push(root.right);
}
if(root.left!=null){
stack.push(root.left);
}
}
return list;
}
递归
class Solution {
List list=new ArrayList<>();
public List inorderTraversal(TreeNode root) {
if(root!=null){
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
}
return list;
}
}
非递归
public List inorderTraversal(TreeNode root){
List list=new ArrayList();
if(root==null){
return list;
}
Stack stack=new Stack<>();
while(root!=null||!stack.isEmpty()){
if(root!=null){
stack.push(root);
root=root.left;
}
else
{
root=stack.pop();
list.add(root.val);
root=root.right;
}
}
return list;
}
递归
class Solution {
List list=new ArrayList<>();
public List postorderTraversal(TreeNode root) {
if(root!=null){
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
}
return list;
}
}
非递归
class Solution {
public List postorderTraversal(TreeNode root) {
List list= new ArrayList();
if(root == null)
return list;
Stack stack = new Stack();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
if(node.left != null){
stack.push(node.left);
}
if(node.right != null) {
stack.push(node.right);
}
list.add(0,node.val); //逆序添加结点值
}
return list;
}
}
class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null){
return true;
}
if(p!=null&&q!=null&&p.val==q.val){
return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
else{
return false;
}
}
}
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s==null){
return false;
}
//t是s的子树,要么t等于s,要么t等于s的左/右子树。
return subFrom(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
}
public boolean subFrom(TreeNode s, TreeNode t){
if (s == null && t == null) return true;
if (s == null || t == null) return false;
if (s.val != t.val) return false;
return subFrom(s.left, t.left) && subFrom(s.right, t.right);
}
}
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
平衡二叉树:节点度差的绝对值不超过1
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
// 左右子树的高度差的绝对值超过 1
if (Math.abs(height(root.left) - height(root.right)) > 1) {
return false;
}
// 分别递归判断左右子树
if (!isBalanced(root.left)) {
return false;
}
return isBalanced(root.right);
}
// 求节点的高度
public int height(TreeNode root) {
if (root == null) {
return 0;
} else {
int left = height(root.left);
int right = height(root.right);
return Math.max(left,right) + 1;
}
}
}
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) {
return true;
}
return cmp(root.left, root.right);
}
public boolean cmp(TreeNode node1, TreeNode node2) {
if (node1 == null && node2 == null) {
return true;
}
//这些情况都不会是对称的
if (node1 == null || node2 == null || node1.val != node2.val) {
return false;
}
return cmp(node1.left, node2.right) && cmp(node1.right, node2.left);
}
}