美文网首页
Sum of leaf node for an N-ary tr

Sum of leaf node for an N-ary tr

作者: 尚无花名 | 来源:发表于2019-05-19 02:28 被阅读0次

    Give an n-ary tree, find the sum of all leaf nodes
    首先这题要先确认treeNode的定义

    class TreeNode {
      int val;
      List<TreeNode> children;
    }
    

    这题就很简单就用DFS recursion来做一下就好了。

    解答如下。

    class Solution {
      public int leafSum(TreeNode root) {
        //base case:    
        if (root == null) return 0;
        if (root.children == null || root.children.size() == 0) return root.val;
        
        int sum = root.val;
        for (TreeNode child : children) {
           sum += leafSum(child);
        }
        return sum;
      }
    }
    

    也可以用BFS做,代码就有点长。

    public int leafSumBFS(TreeNode root) {
       int sum = 0;
       if (root == null) return 0;
       Queue<TreeNode> queue = new LinkedList<>();
       queue.offer(root);
        while (!queue.isEmpty()) {
           TreeNode node = queue.poll();
           if (node.children == null || node.children.size() == 0) sum += node.val;
           else {
              for (TreeNode child : node.children) {
                queue.offer(child);
              }
           }
       }
       return sum;
    }
    

    Follow up: 现在都是常规题,现在我们要求O(1) space的解法。因为现在的是O(h)的space, h 是height.
    给的条件是可以改TreeNode的定义
    我们就加了一个 parent的指针。
    然后在使用的时候我们加一个prev变量, 来记录我们从哪里来的。

    class TreeNode {
      int val;
      TreeNode left, right, parent;
    }
    class Solution {
      public int leafSum(TreeNode root) {
        int sum = 0;
        TreeNode node = root, prev = null; //用prev变量来分辨从哪里来的 
        while (node != null) {
            if (node.left == null && node.right == null) { // leaf node
              sum += node.val;
              prev = node;
              node = node.parent;//return to parent
            } else {  //non leaf node
               if (prev != null && prev.parent == node) { //从下面来的 
                  if (node.right != null && prev != node.right) { //还可以遍历右孩子
                    node = node.right;
                    prev = node;
                  }  else { //没得遍历了,返回
                    prev = node;
                    node = node.parent;
                  }
              }  else {  //从上面来的
                 prev = node;
                 node = node.left;
              }
            }
        }
      return sum;
      }
    }
    

    Nary tree

    class TreeNode {
      int val;
      TreeNode parent;
      List<TreeNode> children;
    }
    class Solution {
      public int leafSum(TreeNode root) {
        int sum = 0;
        TreeNode node = root, prev = null; //用prev变量来分辨从哪里来的 
        while (node != null) {
            if (node.children == null || node.children.size() == 0) { // leaf node
              sum += node.val;
              prev = node;
              node = node.parent;//return to parent
            } else {  //non leaf node
               if (prev != null && prev.parent == node) { //从下面来的 
                  if (node.children != null && 
                        prev != node.children.get(node.children.size() - 1)) { //还可以遍历右孩子
                    int i = 0;
                    for (; i < node.children.size(); i++) {
                      if (prev == node.children.get(i)) break;
                    }
                    prev = node;
                    node = node.children.get(i + 1);
                  }  else { //没得遍历了,返回
                    prev = node;
                    node = node.parent;
                  }
              }  else {  //从上面来的
                 prev = node;
                 node = node.children.get(0);
              }
            }
        }
      return sum;
      }
    }
    

    相关文章

      网友评论

          本文标题:Sum of leaf node for an N-ary tr

          本文链接:https://www.haomeiwen.com/subject/toffzqtx.html