dfs

一些dfs的集合:


Symmetric Tree:

1. 左边val==右边val

2.左边的左边 same 右边的右边;左边的右边 same 右边的左边


public class Solution {
    public boolean isSymmetric(TreeNode root) {
    if (root==null) return true;
    return isSame(root.left,root.right);
    }
    
    private boolean isSame(TreeNode left, TreeNode right) {
        if (left==null) return right==null;
        if (right==null) return false;
        return (left.val==right.val && isSame(left.left, right.right) && isSame(left.right, right.left));
    }

Sum Root to Leaf Numbers:

public class Solution {
    public int sumNumbers(TreeNode root) {
        if (root==null) return 0;
        int[] sum = new int[1];
        helper(root, 0, sum);
        return sum[0];
    }
    
    private void helper(TreeNode root, int pSum, int[] sum) {
        if (root==null) return;
        pSum = pSum * 10 + root.val;
        if (root.left==null && root.right==null) {
            sum[0] +=pSum;
            return;
        } 
        helper(root.left, pSum, sum);
        helper(root.right, pSum, sum);
    }


min depth, max depth:

    public int maxDepth(TreeNode root) {
        if (root==null) return 0;
        return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
    }

    public int minDepth(TreeNode root) {
        if (root==null) return 0;
        if (root.left==null) return minDepth(root.right)+1;
        if (root.right==null) return minDepth(root.left)+1;
        return Math.min(minDepth(root.left), minDepth(root.right))+1;
    }


Balanced Binary Tree: 

   public boolean isBalanced(TreeNode root) {
        if (root==null) return true;
        return isBalanced(root.left)&&isBalanced(root.right)&&
        (Math.abs(depth(root.left)-depth(root.right)))<=1;
    }
    
    private int depth(TreeNode root) {
        if (root==null) return 0;
        return Math.max(depth(root.left),depth(root.right))+1;
    }



Flatten Binary Tree to Linked List: 

* preorder traversal

    public void flatten(TreeNode root) {
        if (root==null) return;
        ArrayList<TreeNode> pre = new ArrayList<TreeNode>();
        pre.add(null);
        helper(root, pre);
    }
    
    private void helper(TreeNode root, List<TreeNode> pre) {
        if (root==null) return;
        TreeNode right = root.right;
        if (pre.get(0)!=null) {
            pre.get(0).left = null;
            pre.get(0).right = root;
        }
        pre.set(0, root);
        helper(root.left, pre);
        helper(right, pre);
    }

Convert Sorted List to Binary Search Tree:

* 先traverse一遍list,计算list长度

* 按照inorder traversal的顺序按binary构建node <===这个方法的重点是要知道总长度!

    public TreeNode sortedListToBST(ListNode head) {
        if (head==null) return null;
        int count = 0;
        ListNode node = head;
        while (node!=null) {
            count++;
            node=node.next;
        }
        List<ListNode> h = new ArrayList<ListNode>();
        h.add(head);
        return helper(h, 0, count-1);
    }
    
    private TreeNode helper(List<ListNode> h, int l, int r) {
        if (l>r) return null;
        int m = (l+r)/2;
        TreeNode left = helper(h, l, m-1);
        TreeNode root = new TreeNode(h.get(0).val);
        h.set(0, h.get(0).next);
        TreeNode right = helper(h, m+1, r);
        root.left=left;
        root.right=right;
        return root;
    }


clone graph dfs 方法:

* 注意避免重复处理node: 如果map里已经有了就不用recursive call了

    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if (node==null) return null;
        HashMap<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
        UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
        map.put(node, newNode);
        helper(node, map);
        return newNode;
    }
    
    private void helper(UndirectedGraphNode ori, HashMap<UndirectedGraphNode,UndirectedGraphNode> map) {
        if (ori==null) return;
        UndirectedGraphNode newNode = map.get(ori);
        for (UndirectedGraphNode next: ori.neighbors) {
            boolean contains = map.containsKey(next);
            if (!contains) map.put(next, new UndirectedGraphNode(next.label));
            newNode.neighbors.add(map.get(next));
            if (!contains) helper(next, map);
        }
    }

Binary Tree Maximum Path Sum:

* 这里利用return值返回包含此node的max path sum (res = max of right+val, left+val, val)

* global max sum = max of sum[0], res, left+right+root.val

* 注意要先把sum[0]=min_value (要先问清楚情况)

    public int maxPathSum(TreeNode root) {
        if (root==null) return 0;
        int[] sum = new int[1];
        sum[0]=Integer.MIN_VALUE;
        helper(root, sum);
        return sum[0];
    }
    
    private int helper(TreeNode root, int[] sum) {
        if (root==null) return 0;
        int left = helper(root.left, sum);
        int right = helper(root.right, sum);
        int res = Math.max(left>0?left:0, right>0?right:0) + root.val;
        sum[0] = Math.max(sum[0], Math.max(res, left+right+root.val));
        return res;
    }

Construct Binary Tree from Preorder and Inorder Traversal

*        root.left = helper(preorder,inorder,pl+1,pl+i-il,il,i-1,map);
*        root.right = helper(preorder,inorder,pl+i-il+1,ph,i+1,ih,map);

Construct Binary Tree from Inorder and Postorder Traversal

*        root.left = helper(inorder,inL,index-1,postorder,postL,postL+index-inL-1,map);
*        root.right = helper(inorder,index+1,inR,postorder,postL+index-inL,postR-1,map);



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值