当前位置:网站首页>2022.5.30-----leetcode. one thousand and twenty-two

2022.5.30-----leetcode. one thousand and twenty-two

2022-06-10 04:45:00 Lu 727

// Simulated stack recursion   
int ans=0;
    String s="";
    public int sumRootToLeaf(TreeNode root) {
       dfs(root);
        return ans;
    }
    void dfs(TreeNode root){
        s+=root.val;
        if(root.left==null&&root.right==null)
            ans+=Integer.parseInt(s,2);
        if(root.left!=null){
            dfs(root.left);
            s=s.substring(0,s.length()-1);
        }
        if(root.right!=null){
            dfs(root.right);
            s=s.substring(0,s.length()-1);
        }
            
    }

原网站

版权声明
本文为[Lu 727]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091228491730.html