Leetcode Tree 基础题

Same tree
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null)return q==null;
        if(q==null)return p==null;
        return p.val==q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

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.

public class Solution {
    int max=0;
    public int maxDepth(TreeNode root) {
        helper(root, 0);
        return max;
    }

    void helper(TreeNode node, int depth)
    {
        if(node==null)return;
        depth++;
        max=Math.max(max, depth);
        helper(node.left, depth);
        helper(node.right, depth);
    }
}

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 of every node never differ by more than 1.

public class Solution {
    public boolean isBalanced(TreeNode root) {
        return helper(root)>=0;
    }

    int helper(TreeNode node)
    {
        if(node==null)return 0;
        int left=helper(node.left);
        if(left==-1)return -1;
        int right=helper(node.right);
        if(right==-1)return -1;
        if(Math.abs(left-right)<2)
        {
            return Math.max(left, right)+1;
        }
        else return -1;
    }
}

Minimum depth of binary tree

public class Solution {
    int min;
    public int minDepth(TreeNode root) {
           min=Integer.MAX_VALUE;
           if(root==null)return 0;
           helper(root, 0);
           return min;
    }

    void helper(TreeNode node, int depth)
    {
        if(node==null)return;
        depth++;
        if(depth>=min)return;
        if(node.left==null && node.right==null)min=Integer.min(min, depth);
        helper(node.left, depth);
        helper(node.right, depth);
    }
}

Flatten binary tree to linked list
Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1
        / \
       2   5
      / \   \
     3   4   6

The flattened tree should look like:

   1
    \
     2
      \
       3
        \
         4
          \
           5
            \
             6

[code]

public class Solution {
    public void flatten(TreeNode root) {
        helper(root);
    }
    TreeNode helper(TreeNode node)
    {
        if(node==null)return null;
        TreeNode left=helper(node.left), right=helper(node.right);
        if(node.left!=null)
        {
            left.right=node.right;
            node.right=node.left;
            node.left=null;
            return right==null ? left : right;
        }
        else return right==null ? node : right;
    }
}

Binary tree maximum path sum
Given a binary tree, find the maximum path sum.

The path may start and end at any node in the tree.

For example:
Given the below binary tree, Return 6.

       1
      / \
     2   3

[code]

public class Solution {
    int max;
    public int maxPathSum(TreeNode root) {
        max=Integer.MIN_VALUE;
        singlePath(root);
        return max;
    }
    int singlePath(TreeNode node)
    {
        if(node==null)return 0;
        int left=singlePath(node.left), right=singlePath(node.right);
        int singleMax=Math.max(0, Math.max(left, right)+node.val);
        max=Math.max(max, left+right+node.val);
        return singleMax;
    }
}

Sum root to leaf numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which represents the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
   / \
  2   3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.

[code]

public class Solution {
    int sum=0;
    public int sumNumbers(TreeNode root) {
        helper(root, 0);
        return sum;
    }
    void helper(TreeNode node, int local)
    {
        if(node==null)return;
        int inc=local*10+node.val;
        if(node.left==null && node.right==null)
        {
            sum+=inc;
        }
        helper(node.left, inc);
        helper(node.right, inc);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值