📜  找到所有左叶节点的总和,它也有它的右兄弟节点

📅  最后修改于: 2022-05-13 01:56:06.048000             🧑  作者: Mango

找到所有左叶节点的总和,它也有它的右兄弟节点

给定一个以root 为根的二叉树,任务是找到所有叶子节点的总和,这些叶子节点是其父节点的左子节点并且也有右兄弟节点,即父节点也有右子节点。

例子

方法:这个想法是使用递归来解决这个问题。从根节点开始遍历。对于每个节点检查它是否为NULL ,如果为真则返回 0。 如果两个孩子都是NULL返回0 。如果两个孩子都不是NULL

  • 如果 left 是叶节点,则返回 root->left->data + value 遍历右子节点。
  • 遍历左孩子的其他返回值+遍历右孩子的值

按照以下步骤解决问题:

  • 如果root等于null或叶节点,则返回0。
  • 如果root有两个孩子,则执行以下任务:
    • 如果root的左孩子是叶节点,则返回root->left->data + sumOfLeft(root->right)。
    • 否则,返回sumofLeft(root->left)sumofLeft(root->right) 的总和。
  • 否则,如果存在root->left,则返回sumofLeft(root->left)。
  • 否则返回sumofLeft(root->right)。

下面是上述方法的实现

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Node of the binary tree.
class Node {
public:
    int data;
    Node *left, *right;
    Node(int data)
    {
        this->data = data;
        this->left = NULL;
        this->right = NULL;
    }
};
 
// Function to return the sum of
// left leaf node having right sibling.
int sumOfLeft(Node* root)
{
 
    // If root node is NULL
    if (root == NULL)
        return 0;
 
    // If root node is a leaf node
    // Note: It is not for left leaf
    // node having right sibling
    if (root->left == NULL
        && root->right == NULL)
        return 0;
 
    // If node has both the child
    if (root->left != NULL
        && root->right != NULL) {
 
        // If its left node is leaf node
        if (root->left->left == NULL
            && root->left->right == NULL) {
 
            // Returning the sum of
            // left node data
            // and the value obtained by
            // traversing right child
            return root->left->data
                   + sumOfLeft(root->right);
        }
        else {
 
            // Returning sum of values
            // obtained by traversing
            // both the child
            return sumOfLeft(root->left)
                   + sumOfLeft(root->right);
        }
    }
 
    // If there is only left child
    else if (root->left != NULL) {
        return sumOfLeft(root->left);
    }
 
    // If only right child is left
    return sumOfLeft(root->right);
}
 
// Driver Code
int main()
{
    // Binary tree construction
    Node* root = new Node(12);
    root->left = new Node(13);
    root->right = new Node(10);
    root->right->left = new Node(14);
    root->right->right = new Node(15);
    root->right->right->left
        = new Node(22);
    root->right->right->right
        = new Node(23);
    cout << sumOfLeft(root);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Node of the binary tree.
static class Node {
    int data;
    Node left, right;
    Node(int data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
};
 
// Function to return the sum of
// left leaf node having right sibling.
static int sumOfLeft(Node root)
{
 
    // If root node is null
    if (root == null)
        return 0;
 
    // If root node is a leaf node
    // Note: It is not for left leaf
    // node having right sibling
    if (root.left == null
        && root.right == null)
        return 0;
 
    // If node has both the child
    if (root.left != null
        && root.right != null) {
 
        // If its left node is leaf node
        if (root.left.left == null
            && root.left.right == null) {
 
            // Returning the sum of
            // left node data
            // and the value obtained by
            // traversing right child
            return root.left.data
                   + sumOfLeft(root.right);
        }
        else {
 
            // Returning sum of values
            // obtained by traversing
            // both the child
            return sumOfLeft(root.left)
                   + sumOfLeft(root.right);
        }
    }
 
    // If there is only left child
    else if (root.left != null) {
        return sumOfLeft(root.left);
    }
 
    // If only right child is left
    return sumOfLeft(root.right);
}
 
// Driver Code
public static void main(String[] args)
{
    // Binary tree construction
    Node root = new Node(12);
    root.left = new Node(13);
    root.right = new Node(10);
    root.right.left = new Node(14);
    root.right.right = new Node(15);
    root.right.right.left
        = new Node(22);
    root.right.right.right
        = new Node(23);
    System.out.print(sumOfLeft(root));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Node of the binary tree.
class Node:
    def __init__(self, data):
        self.data = data;
        self.left = None;
        self.right = None;
 
# Function to return the sum of
# left leaf Node having right sibling.
def sumOfLeft(root):
 
    # If root Node is None
    if (root == None):
        return 0;
 
    # If root Node is a leaf Node
    # Note: It is not for left leaf
    # Node having right sibling
    if (root.left == None and root.right == None):
        return 0;
 
    # If Node has both the child
    if (root.left != None and root.right != None):
 
        # If its left Node is leaf Node
        if (root.left.left == None and root.left.right == None):
 
            # Returning the sum of
            # left Node data
            # and the value obtained by
            # traversing right child
            return root.left.data + sumOfLeft(root.right);
        else:
 
            # Returning sum of values
            # obtained by traversing
            # both the child
            return sumOfLeft(root.left) + sumOfLeft(root.right);
         
    # If there is only left child
    elif(root.left != None):
        return sumOfLeft(root.left);
     
    # If only right child is left
    return sumOfLeft(root.right);
 
# Driver Code
if __name__ == '__main__':
   
    # Binary tree construction
    root =  Node(12);
    root.left =  Node(13);
    root.right =  Node(10);
    root.right.left =  Node(14);
    root.right.right =  Node(15);
    root.right.right.left =  Node(22);
    root.right.right.right =  Node(23);
    print(sumOfLeft(root));
 
# This code is contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
public class GFG{
 
  // Node of the binary tree.
  class Node {
    public int data;
    public Node left, right;
    public Node(int data)
    {
      this.data = data;
      this.left = null;
      this.right = null;
    }
  };
 
  // Function to return the sum of
  // left leaf node having right sibling.
  static int sumOfLeft(Node root)
  {
 
    // If root node is null
    if (root == null)
      return 0;
 
    // If root node is a leaf node
    // Note: It is not for left leaf
    // node having right sibling
    if (root.left == null
        && root.right == null)
      return 0;
 
    // If node has both the child
    if (root.left != null
        && root.right != null) {
 
      // If its left node is leaf node
      if (root.left.left == null
          && root.left.right == null) {
 
        // Returning the sum of
        // left node data
        // and the value obtained by
        // traversing right child
        return root.left.data
          + sumOfLeft(root.right);
      }
      else {
 
        // Returning sum of values
        // obtained by traversing
        // both the child
        return sumOfLeft(root.left)
          + sumOfLeft(root.right);
      }
    }
 
    // If there is only left child
    else if (root.left != null) {
      return sumOfLeft(root.left);
    }
 
    // If only right child is left
    return sumOfLeft(root.right);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // Binary tree construction
    Node root = new Node(12);
    root.left = new Node(13);
    root.right = new Node(10);
    root.right.left = new Node(14);
    root.right.right = new Node(15);
    root.right.right.left
      = new Node(22);
    root.right.right.right
      = new Node(23);
    Console.Write(sumOfLeft(root));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript



输出
49

时间复杂度: O(N),其中 N 是节点总数
辅助空间: O(1)