二叉树从左向右访问节点_二叉树的最左和最右节点

二叉树从左向右访问节点

Problem statement:

问题陈述:

Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost.

给定二叉树,在每个级别上打印角节点。 最左边的节点和最右边的节点。

Example:

例:

Leftmost and Rightmost Nodes of Binary Tree

For this tree output will be: 2 7 5 2 9 5 4

对于此树,输出为: 2 7 5 2 9 5 4

Solution:

解:

Of course the solution includes level order traversal.

当然,解决方案包括级别顺序遍历

Algorithm:

算法:

FUNCTION printCorner (root): prints corner nodes (leftmost, rightmost) of the tree

FUNCTION printCorner(根) :打印树的角节点(最左,最右)

Prerequisite: Queue q, input binary tree root

先决条件: 队列q ,输入二叉树根

FUNCTION printCorner(Node *root)
1.  Declare a queue to store pointer to tree nodesq;
2.  Declare store=0 which will print the rightmost node;
3.  Declare temp to store DeQueued element from queue ;
4.  Declare count=0 which will help to print the leftmost node;
5.  Print the root->data first
6.  IF (root->left)
        EnQueue (q,root->left);
	IF (root->right)
	    EnQueue (q,root->right);
	    EnQueue (q, NULL);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值