zoukankan      html  css  js  c++  java
  • [Algo] 646. Store Number Of Nodes In Left Subtree

    Given a binary tree, count the number of nodes in each node’s left subtree, and store it in the numNodesLeft field.

    Examples

                      1(6)

                   /         

               2(3)        3(0)

              /     

          4(1)     5(0)

        /               

    6(0)     7(0)   8(0)

    The numNodesLeft is shown in parentheses.

     

    /**
     * public class TreeNodeLeft {
     *   public int key;
     *   public TreeNodeLeft left;
     *   public TreeNodeLeft right;
     *   public int numNodesLeft;
     *   public TreeNodeLeft(int key) {
     *     this.key = key;
     *   }
     * }
     */
    public class Solution {
      public void numNodesLeft(TreeNodeLeft root) {
        // Write your solution here
        helper(root);
      }
    
      private int helper(TreeNodeLeft root) {
        if (root == null) {
          return 0;
        }
        int left = helper(root.left);
        int right = helper(root.right);
        root.numNodesLeft = left;
        return 1 + left + right;
      }
    }
  • 相关阅读:
    右滑返回上一页
    flutter 启动图
    [题解]NOIP2014
    [题解]LightOJ1289 LCM from 1 to n
    [题解]CodeForces442B Andrey and Problem
    [题解]HDU4035 Maze
    [题解]CodeForces#290(div1)
    SCP-bzoj-1078
    SCP-bzoj-1068
    SCP-bzoj-1054
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12340807.html
Copyright © 2011-2022 走看看