[Algo] 646. Store Number Of Nodes In Left Subtree

Posted xuanlu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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;
  }
}

 

以上是关于[Algo] 646. Store Number Of Nodes In Left Subtree的主要内容,如果未能解决你的问题,请参考以下文章

053-646

e646. 处理鼠标点击事件

Codeforces Round #646 (Div. 2)题解

Codeforces Round #646 (Div. 2)题解

codeforces Round 646(div. 2)

646. Maximum Length of Pair Chain