Skip to content

Latest commit

 

History

History
105 lines (87 loc) · 3.23 KB

563_二叉树的坡度.md

File metadata and controls

105 lines (87 loc) · 3.23 KB
import java.util.*;

/**
 * @author : xfhy
 * Create time : 2020年12月29日08:17:09
 * Description : 563. 二叉树的坡度
 * source : https://leetcode-cn.com/problems/binary-tree-tilt/
 */
public class Solution {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

        @Override
        public String toString() {
            return "TreeNode{" +
                    "val=" + val +
                    ", left=" + left +
                    ", right=" + right +
                    '}';
        }
    }

    /**
     * 思路1 递归: 计算当前节点的左子树与右子树之差,再计算当前节点的左子树的坡度,再计算当前节点的右子树的坡度,然后加起来.
     */
    public static int findTilt(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.abs(subtreeAnd(root.left) - subtreeAnd(root.right)) + findTilt(root.left) + findTilt(root.right);
    }

    public static int subtreeAnd(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return root.val + subtreeAnd(root.left) + subtreeAnd(root.right);
    }

    //思路2: 二叉树的每个节点都调用一下traverse,traverse将该节点的左右子树和(在里面需要计算以当前节点的二叉树之和)的差(坡度)计算出来,然后加到result(坡度最后的结果)里面.
    private static int result = 0;

    public static int findTilt2(TreeNode root) {
        traverse(root);
        return result;
    }

    private static int traverse(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = traverse(root.left);
        int right = traverse(root.right);

        //将当前节点的左右子树和的差(坡度)计算出来,然后加到result里面
        result += Math.abs(left - right);

        //计算以当前节点为根节点的二叉树之和
        return left + right + root.val;
    }


    public static TreeNode createBinaryTree(LinkedList<Integer> inputList) {
        if (inputList == null || inputList.isEmpty()) {
            return null;
        }
        TreeNode node = null;
        Integer data = inputList.removeFirst();
        if (data != null) {
            node = new TreeNode(data);
            node.left = createBinaryTree(inputList);
            node.right = createBinaryTree(inputList);
        }
        return node;
    }

    public static void main(String[] args) {
        /*LinkedList<Integer> integers = new LinkedList<>(Arrays.asList(5, 4, 11, 7, null, null, 2, null, null, null, 8, 13, null, null, 4,
                5, null, null, 1));*/
        /*LinkedList<Integer> integers = new LinkedList<>(Arrays.asList(3, 2, 9, null, null, 10, null,
                null, 8, null, 4));*/
        LinkedList<Integer> integers = new LinkedList<>(Arrays.asList(4, 2, 3, null, null, 5, null, null, 9, null, 7));
        TreeNode binaryTree = createBinaryTree(integers);

        int res = findTilt2(binaryTree);
        System.out.println(res);
    }

}