原文:

给定一棵二叉树,任务是从左到右打印给定二叉树的所有叶节点。也就是说,节点应该按照它们在给定树中从左到右的顺序打印。 例:

input : 
           1
          /  \
         2    3
        / \  / \
       4  5  6  7
output : 4 5 6 7
input :
            4
           /  \
          5    9
         / \  / \
        8   3 7  2
       /         / \
      12        6   1
output : 12 3 7 6 1

我们已经讨论了使用两个堆栈的方法。 方法:想法是使用一个堆栈执行迭代

c

// c   program to print leaf nodes from
// left to right using one stack
#include 
using namespace std;
// structure of binary tree
struct node {
    node* left;
    node* right;
    int data;
};
// function to create a new node
node* newnode(int key)
{
    node* node = new node();
    node->left = node->right = null;
    node->data = key;
    return node;
}
// function to print all the leaf nodes
// of binary tree using one stack
void printleaflefttoright(node* p)
{
    // stack to store the nodes
    stack s;
    while (1) {
        // if p is not null then push
        // it on the stack
        if (p) {
            s.push(p);
            p = p->left;
        }
        else {
            // if stack is empty then come out
            // of the loop
            if (s.empty())
                break;
            else {
                // if the node on top of the stack has its
                // right subtree as null then pop that node and
                // print the node only if its left
                // subtree is also null
                if (s.top()->right == null) {
                    p = s.top();
                    s.pop();
                    // print the leaf node
                    if (p->left == null)
                        printf("%d ", p->data);
                }
                while (p == s.top()->right) {
                    p = s.top();
                    s.pop();
                    if (s.empty())
                        break;
                }
                // if stack is not empty then assign p as
                // the stack's top node's right child
                if (!s.empty())
                    p = s.top()->right;
                else
                    p = null;
            }
        }
    }
}
// driver code
int main()
{
    node* root = newnode(1);
    root->left = newnode(2);
    root->right = newnode(3);
    root->left->left = newnode(4);
    root->left->right = newnode(5);
    root->right->left = newnode(6);
    root->right->right = newnode(7);
    printleaflefttoright(root);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print leaf nodes from
// left to{ right using one stack
import java.util.*;
class gfg
{
// structure of binary tree
static class node
{
    node left;
    node right;
    int data;
}
// function to create a new node
static node newnode(int key)
{
    node node = new node();
    node.left = null;
    node.right = null;
    node.data = key;
    return node;
}
// function to print all the leaf nodes
// of binary tree using one stack
static void printleaflefttoright(node p)
{
    // stack to store the nodes
    stack s = new stack ();
    while (true)
    {
        // if p is not null then push
        // it on the stack
        if (p != null)
        {
            s.push(p);
            p = p.left;
        }
        else
        {
            // if stack is empty then come out
            // of the loop
            if (s.isempty())
                break;
            else
            {
                // if the node on top of the stack has its
                // right subtree as null then pop that node and
                // print the node only if its left
                // subtree is also null
                if (s.peek().right == null)
                {
                    p = s.peek();
                    s.pop();
                    // print the leaf node
                    if (p.left == null)
                        system.out.print(p.data   " ");
                }
                while (p == s.peek().right)
                {
                    p = s.peek();
                    s.pop();
                    if (s.isempty())
                        break;
                }
                // if stack is not empty then assign p as
                // the stack's top node's right child
                if (!s.isempty())
                    p = s.peek().right;
                else
                    p = null;
            }
        }
    }
}
// driver code
public static void main(string[] args)
{
    node root = newnode(1);
    root.left = newnode(2);
    root.right = newnode(3);
    root.left.left = newnode(4);
    root.left.right = newnode(5);
    root.right.left = newnode(6);
    root.right.right = newnode(7);
    printleaflefttoright(root);
}
}
// this code is contributed by prerna saini

python 3

# python3 program to print leaf nodes from
# left to right using one stack
# binary tree node
class newnode:
    def __init__(self, data):
        self.data = data
        self.left = none
        self.right = none
# function to print all the leaf nodes
# of binary tree using one stack
def printleaflefttoright(p):
    # stack to store the nodes
    s = []
    while (1):
        # if p is not none then push
        # it on the stack
        if (p):
            s.insert(0, p)
            p = p.left
        else:
            # if stack is empty then come out
            # of the loop
            if len(s) == 0:
                break
            else:
                # if the node on top of the stack has its
                # right subtree as none then pop that node
                # and print the node only if its left
                # subtree is also none
                if (s[0].right == none):
                    p = s[0]
                    s.pop(0)
                    # print the leaf node
                    if (p.left == none):
                        print(p.data, end = " ")
                while (p == s[0].right):
                    p = s[0]
                    s.pop(0)
                    if len(s) == 0:
                        break
                # if stack is not empty then assign p as
                # the stack's top node's right child
                if len(s):
                    p = s[0].right
                else:
                    p = none
# driver code
root = newnode(1)
root.left = newnode(2)
root.right = newnode(3)
root.left.left = newnode(4)
root.left.right = newnode(5)
root.right.left = newnode(6)
root.right.right = newnode(7)
printleaflefttoright(root)
# this code is contributed by shubhamsingh10

c

// c# program to print leaf nodes from
// left to{ right using one stack
using system;
using system.collections.generic;
class gfg
{
// structure of binary tree
public class node
{
    public node left;
    public node right;
    public int data;
}
// function to create a new node
static node newnode(int key)
{
    node node = new node();
    node.left = null;
    node.right = null;
    node.data = key;
    return node;
}
// function to print all the leaf nodes
// of binary tree using one stack
static void printleaflefttoright(node p)
{
    // stack to store the nodes
    stack s = new stack ();
    while (true)
    {
        // if p is not null then push
        // it on the stack
        if (p != null)
        {
            s.push(p);
            p = p.left;
        }
        else
        {
            // if stack is empty then come out
            // of the loop
            if (s.count == 0)
                break;
            else
            {
                // if the node on top of the stack has its
                // right subtree as null then pop that node and
                // print the node only if its left
                // subtree is also null
                if (s.peek().right == null)
                {
                    p = s.peek();
                    s.pop();
                    // print the leaf node
                    if (p.left == null)
                        console.write(p.data   " ");
                }
                while (p == s.peek().right)
                {
                    p = s.peek();
                    s.pop();
                    if (s.count == 0)
                        break;
                }
                // if stack is not empty then assign p as
                // the stack's top node's right child
                if (s.count != 0)
                    p = s.peek().right;
                else
                    p = null;
            }
        }
    }
}
// driver code
public static void main(string[] args)
{
    node root = newnode(1);
    root.left = newnode(2);
    root.right = newnode(3);
    root.left.left = newnode(4);
    root.left.right = newnode(5);
    root.right.left = newnode(6);
    root.right.right = newnode(7);
    printleaflefttoright(root);
}
}
// this code has been contributed by 29ajaykumar

java 描述语言


output: 

4 5 6 7

时间复杂度:o(n) t3】辅助空间: o(n)