📌  相关文章
📜  二叉树中最深的左叶节点|迭代法

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

二叉树中最深的左叶节点|迭代法

给定一棵二叉树,找到最深的叶子节点,它是其父节点的左子节点。例如,考虑下面的树。最深的左叶节点是值为 9 的节点。
例子:

Input : 
       1
     /   \
    2     3
  /      /  \  
 4      5    6
        \     \
         7     8
        /       \
       9         10


Output : 9

此处讨论此问题的递归方法
对于迭代方法,思路类似于层序遍历的方法二
这个想法是迭代遍历树,每当左树节点被推入队列时,检查它是否是叶节点,如果是叶节点,然后更新结果。由于我们逐级进行,最后存储的叶节点是最深的,

C++
// CPP program to find deepest left leaf
// node of binary tree
#include 
using namespace std;
 
// tree node
struct Node {
    int data;
    Node *left, *right;
};
 
// returns a new tree Node
Node* newNode(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
 
// return the deepest left leaf node
// of binary tree
Node* getDeepestLeftLeafNode(Node* root)
{
    if (!root)
        return NULL;
 
    // create a queue for level order traversal
    queue q;
    q.push(root);
 
    Node* result = NULL;
 
    // traverse until the queue is empty
    while (!q.empty()) {
        Node* temp = q.front();
        q.pop();
 
          
        // Since we go level by level, the last
        // stored left leaf node is deepest one,
        if (temp->left) {
            q.push(temp->left);
            if (!temp->left->left && !temp->left->right)
                result = temp->left;
        }
         
        if (temp->right)
            q.push(temp->right);
    }
    return result;
}
 
// driver program
int main()
{
    // construct a tree
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->right->left = newNode(5);
    root->right->right = newNode(6);
    root->right->left->right = newNode(7);
    root->right->right->right = newNode(8);
    root->right->left->right->left = newNode(9);
    root->right->right->right->right = newNode(10);
 
    Node* result = getDeepestLeftLeafNode(root);
    if (result)
        cout << "Deepest Left Leaf Node :: "
             << result->data << endl;
    else
        cout << "No result, left leaf not found\n";
    return 0;
}


Java
// Java program to find deepest left leaf
// node of binary tree
import java.util.*;
 
class GFG
{
 
// tree node
static class Node
{
    int data;
    Node left, right;
};
 
// returns a new tree Node
static Node newNode(int data)
{
    Node temp = new Node();
    temp.data = data;
    temp.left = temp.right = null;
    return temp;
}
 
// return the deepest left leaf node
// of binary tree
static Node getDeepestLeftLeafNode(Node root)
{
    if (root == null)
        return null;
 
    // create a queue for level order traversal
    Queue q = new LinkedList<>();
    q.add(root);
 
    Node result = null;
 
    // traverse until the queue is empty
    while (!q.isEmpty())
    {
        Node temp = q.peek();
        q.remove();
 
        // Since we go level by level, the last
        // stored left leaf node is deepest one,
        if (temp.left != null)
        {
            q.add(temp.left);
            if (temp.left.left == null &&
                temp.left.right == null)
                result = temp.left;
        }
         
        if (temp.right != null)
            q.add(temp.right);
    }
    return result;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // construct a tree
    Node root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(4);
    root.right.left = newNode(5);
    root.right.right = newNode(6);
    root.right.left.right = newNode(7);
    root.right.right.right = newNode(8);
    root.right.left.right.left = newNode(9);
    root.right.right.right.right = newNode(10);
 
    Node result = getDeepestLeftLeafNode(root);
    if (result != null)
        System.out.println("Deepest Left Leaf Node :: " +
                                            result.data);
    else
        System.out.println("No result, " +
                   "left leaf not found");
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find deepest
# left leaf Binary search Tree
 
_MIN = -2147483648
_MAX = 2147483648
 
# Helper function that allocates a new
# node with the given data and None
# left and right pointers.                                    
class newnode:
 
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# utility function to return deepest
# left leaf node
def getDeepestLeftLeafNode(root) :
 
    if (not root):
        return None
 
    # create a queue for level
    # order traversal
    q = []
    q.append(root)
 
    result = None
 
    # traverse until the queue is empty
    while (len(q)):
        temp = q[0]
        q.pop(0)
 
        if (temp.left):
            q.append(temp.left)
            if (not temp.left.left and
                not temp.left.right):
                result = temp.left
         
        # Since we go level by level,
        # the last stored right leaf
        # node is deepest one
        if (temp.right):
            q.append(temp.right)        
     
    return result
 
# Driver Code
if __name__ == '__main__':
     
    # create a binary tree
    root = newnode(1)
    root.left = newnode(2)
    root.right = newnode(3)
    root.left.Left = newnode(4)
    root.right.left = newnode(5)
    root.right.right = newnode(6)
    root.right.left.right = newnode(7)
    root.right.right.right = newnode(8)
    root.right.left.right.left = newnode(9)
    root.right.right.right.right = newnode(10)
 
    result = getDeepestLeftLeafNode(root)
    if result:
        print("Deepest Left Leaf Node ::",
                              result.data)
    else:
        print("No result, Left leaf not found")
         
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# program to find deepest left leaf
// node of binary tree
using System;
using System.Collections.Generic;
     
class GFG
{
 
// tree node
class Node
{
    public int data;
    public Node left, right;
};
 
// returns a new tree Node
static Node newNode(int data)
{
    Node temp = new Node();
    temp.data = data;
    temp.left = temp.right = null;
    return temp;
}
 
// return the deepest left leaf node
// of binary tree
static Node getDeepestLeftLeafNode(Node root)
{
    if (root == null)
        return null;
 
    // create a queue for level order traversal
    Queue q = new Queue();
    q.Enqueue(root);
 
    Node result = null;
 
    // traverse until the queue is empty
    while (q.Count != 0)
    {
        Node temp = q.Peek();
        q.Dequeue();
 
        // Since we go level by level, the last
        // stored left leaf node is deepest one,
        if (temp.left != null)
        {
            q.Enqueue(temp.left);
            if (temp.left.left == null &&
                temp.left.right == null)
                result = temp.left;
        }
        if (temp.right != null)
            q.Enqueue(temp.right);
    }
    return result;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // construct a tree
    Node root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(4);
    root.right.left = newNode(5);
    root.right.right = newNode(6);
    root.right.left.right = newNode(7);
    root.right.right.right = newNode(8);
    root.right.left.right.left = newNode(9);
    root.right.right.right.right = newNode(10);
 
    Node result = getDeepestLeftLeafNode(root);
    if (result != null)
        Console.WriteLine("Deepest Left Leaf Node :: " +
                                           result.data);
    else
        Console.WriteLine("No result, " +
                  "left leaf not found");
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

Deepest Left Leaf Node :: 9 

?list=PLqM7alHXFySHCXD7r1J0ky9Zg_GBB1dbk