ARTS-12-计算二叉树根到叶子节点之和

简介: ARTS-12-计算二叉树根到叶子节点之和

Algorithm


题目概述:


Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path1->2->3which represents the number123.

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


For example,


1
   / \
  2   3
复制代码


The root-to-leaf path1->2represents the number12.

The root-to-leaf path1->3represents the number13.

Return the sum = 12 + 13 =25.


代码:


/**
 * Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.
 * <p>
 * An example is the root-to-leaf path1->2->3which represents the number123.
 * <p>
 * Find the total sum of all root-to-leaf numbers.
 * <p>
 * For example,
 * The root-to-leaf path1->2represents the number12.
 * The root-to-leaf path1->3represents the number13.
 * <p>
 * Return the sum = 12 + 13 =25.
 *
 * @author idea
 * @data 2019/7/14
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) {
        val = x;
    }
}
public class TotalDemo {
    private StringBuilder stb = new StringBuilder();
    public Integer sum=new Integer(0);
    public int sumNumbers(TreeNode root) {
        StringBuilder stb = new StringBuilder();
        findLeafNodeParent(root, stb);
        return sum;
    }
    /**
     * 查询叶子节点
     *
     * @return
     */
    public void findLeafNodeParent(TreeNode root, StringBuilder current) {
        if (root == null) {
            return;
        }
        current.append(root.val);
        if (isLeaf(root)) {
            sum += Integer.parseInt(current + "");
        }
        findLeafNodeParent(root.left, current);
        findLeafNodeParent(root.right, current);
        current.deleteCharAt(current.length() - 1);
    }
    /**
     * 判断是否是叶子节点
     *
     * @param root
     * @return
     */
    public boolean isLeaf(TreeNode root) {
        return root.left == null && root.right == null;
    }
    public static void main(String[] args) {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(3);
        TreeNode t4 = new TreeNode(4);
        TreeNode t5 = new TreeNode(5);
        TreeNode t6 = new TreeNode(6);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t5.left = t6;
        TotalDemo td = new TotalDemo();
        Integer sum = td.sumNumbers(t1);
        System.out.println(sum);
    }
}


目录
相关文章
【Leetcode -872.叶子相似的树 -993.二叉树的堂兄弟节点】
【Leetcode -872.叶子相似的树 -993.二叉树的堂兄弟节点】
39 0
|
5月前
【树 - 平衡二叉树(AVL)】F(n)=F(n-1)+F(n-2)+1 这个类似于一个递归的数列,1是根节点,F(n-1)是左子树的节点数量,F(n-2)是右子树的节点数量
【树 - 平衡二叉树(AVL)】F(n)=F(n-1)+F(n-2)+1 这个类似于一个递归的数列,1是根节点,F(n-1)是左子树的节点数量,F(n-2)是右子树的节点数量
|
存储 算法 数据库管理
树和二叉树(二)
树和二叉树(二)
|
6月前
|
存储 算法
哈夫曼树(赫夫曼树、最优树)详解
哈夫曼树(赫夫曼树、最优树)详解
119 0
计算左子树规模(结点个数),找出树的根结点
简单的计算公式教你找出左子树到底有多少个娃,也会与你一起寻找根结点,快来看看呀
计算左子树规模(结点个数),找出树的根结点
C++求根节点到叶子节点数字之和
C++求根节点到叶子节点数字之和
二叉树的前中后序遍历以及求深度、叶子节点和二叉树的重建
二叉树的前中后序遍历以及求深度、叶子节点和二叉树的重建
74 0
|
存储 机器学习/深度学习 算法
九、树和二叉树
九、树和二叉树
九、树和二叉树
二叉树的层序遍历、二叉树叶节点输出算法、求二叉树的高度、层序创建一棵二叉树
二叉树的层序遍历、二叉树叶节点输出算法、求二叉树的高度、层序创建一棵二叉树