Given a binary tree, print boundary nodes of the binary tree Anti-Clockwise starting from the root. For example, boundary traversal of the following tree is “20 8 4 10 14 25 22″
We break the problem in 3 parts:
1. Print the left boundary in top-down manner.
2. Print all leaf nodes from left to right, which can again be sub-divided into two sub-parts:
…..2.1 Print all leaf nodes of left sub-tree from left to right.
…..2.2 Print all leaf nodes of right subtree from left to right.
3. Print the right boundary in bottom-up manner.
We need to take care of one thing that nodes are not printed again. e.g. The left most node is also the leaf node of the tree.
Based on the above cases, below is the implementation:
// A simple function to print leaf nodes of a binary tree void printLeaves(struct node* root) { if ( root ) { printLeaves(root->left); // Print it if it is a leaf node if ( !(root->left) && !(root->right) ) printf("%d ", root->data); printLeaves(root->right); } } // A function to print all left boundry nodes, except a leaf node. // Print the nodes in TOP DOWN manner void printBoundaryLeft(struct node* root) { if (root) { if (root->left) { // to ensure top down order, print the node // before calling itself for left subtree printf("%d ", root->data); printBoundaryLeft(root->left); } else if( root->right ) { printf("%d ", root->data); printBoundaryLeft(root->right); } // do nothing if it is a leaf node, this way we avoid // duplicates in output } } // A function to print all right boundry nodes, except a leaf node // Print the nodes in BOTTOM UP manner void printBoundaryRight(struct node* root) { if (root) { if ( root->right ) { // to ensure bottom up order, first call for right // subtree, then print this node printBoundaryRight(root->right); printf("%d ", root->data); } else if ( root->left ) { printBoundaryRight(root->left); printf("%d ", root->data); } // do nothing if it is a leaf node, this way we avoid // duplicates in output } } // A function to do boundary traversal of a given binary tree void printBoundary (struct node* root) { if (root) { printf("%d ",root->data); // Print the left boundary in top-down manner. printBoundaryLeft(root->left); // Print all leaf nodes printLeaves(root->left); printLeaves(root->right); // Print the right boundary in bottom-up manner printBoundaryRight(root->right); } }
References:
http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/
http://articles.leetcode.com/2010/10/print-edge-nodes-boundary-of-binary.html
相关推荐
### 边界检测在图像篡改区域中的应用 #### 概述 随着数字技术的不断发展,图像编辑软件的功能越来越强大,使得人们可以轻松地对图片进行编辑甚至拼接,这导致了一个新的问题:如何判断一张图片的真实性和完整性。...
拉格朗日边界问题(Lagrangian boundary problem)则是在系统动力学中,对运动轨迹的初始和结束条件施加约束的问题。 塞福特猜测(Seifert conjecture)是一个与哈密顿系统相关的数学问题,由Heinz Seifert在1948年...
本篇论文主要介绍了一种基于磷的非平衡晶界偏聚(NGS)动力学模型的晶界脆性预报方法。这一方法由李庆芬和李莉提出,目的在于利用磷的非平衡晶界偏聚机制对钢材料晶界脆性进行预报,特别针对的是12Cr1MoV和2.25Cr1Mo两...
在本文中,我们将深入探讨boundary数据解析的原理,以及如何通过代码实现这一过程。 首先,理解“boundary”是什么至关重要。在多部分表单数据中,boundary是一个特殊的字符串,用于分割不同的数据部分。例如,当...
文章中还提到了研究资金来源,即国家自然科学基金(NSF of P.R. China),这表明这项研究可能得到了国家科学基金的资助。此外,文中还提到了作者高璐和张志军,分别来自烟台大学数学与信息科学学院,代表了中国在...
带非阻尼和扩散的非线性发展方程零扩散极限的边界层,彭红云,阮立志,文考虑某种带阻尼和扩散的非线性发展方程,主要研究了当扩散参数β趋于0时的边界层效应和收敛率,并给出了边界层厚度的阶数O(βr)(0<...
explanation of the Boundary Element Method (BEM), that is easy for engineers and scientists to follow, is retained. This is achieved by explaining some aspects of the method in an engineering rather ...
this paper presents a novel method of buffer generation based on vector boundary tracing. The new method can avoid complex vector calculations, such as line and curve segment intersection, clipping ...
Boundary fitting of extracted objects using LIFS Boundary Fitting of Extracted Objects Using LIFS Takashi Ida, Yoko Sambonsugi, and Toshiaki Watanabe Research and Development Center, Toshiba ...
磷在晶界偏聚现象的研究是材料科学领域重要的课题之一,尤其对于低合金钢等材料的微观结构和宏观性能有着密切关系。晶界偏聚是指溶质原子在晶界区域富集的现象,这主要影响材料的力学、化学以及电子性质,同时还可能...
型 A 仿射 Weyl 群典范左胞腔的下界超平面,时俭益,,该文考虑型 A 仿射 Weyl 群的典范左胞腔的下界超平面集合, 讨论这个超平面集合在多大程度上确定了相应的典范左胞腔, 从而回答了Humphrey
本文研究了一种具有高加速度/减速度轴向移动系统的边界控制问题。研究的系统为轴向移动皮带,在空间变化张力和未知扰动作用下,其边界振动抑制是研究的重点。为了解决皮带振动并避免控制溢出问题,文章从扩展哈密顿...