Vous êtes sur la page 1sur 27

6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

This is Google's cache of 
Google http://articles.leetcode.com/print­edge­nodes­boundary­of­binary/. It is a snapshot of the page
as it appeared on May 26, 2019 20:15:40 GMT. The current page could have changed in the meantime. Learn more.

Full version Text­only version View source


Tip: To quickly find your search term on this page, press Ctrl+F or ⌘­F (Mac) and use the find bar.

Skip to toolbar
Log in
Search Search

Login
Home
About
Discuss
Members
Online Judge
LeetCode

Print Edge Nodes (Boundary) of a Binary Tree


October 20, 2010 by 1337c0d3r  49 Replies

Print all edge nodes of a complete binary tree anti­clockwise. 
That is all the left most nodes starting at root, then the leaves left to right and finally all the
rightmost nodes. 
In other words, print the boundary of the tree.

Variant: Print the same for a tree that is not complete.

Thanks to one reader who contributed this interesting question.

Some Examples:
** Please see Further Thoughts section below for an example which points out an ambiguity found in the problem statement. **

Assume we have a binary tree below:

    _30_
   /   \   
  10    20
 /     /  \
50    45  35

The correct solution should print 30, 10, 50, 45, 35, 20.
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 1/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

Another binary tree:

______30______
/ \
__20__ __40__
/ \ / \
10 25 35 50
/ \ \ /
5 15 28 41

The correct solution should print 30, 20, 10, 5, 15, 28, 35, 41, 50, 40.

Hint:
To solve this problem, you first need to pay attention to these three key areas:

Printing the leftmost edges from top to bottom. When a node is a leftmost edge, its left child must also be a leftmost
edge.
Printing the leaves from left to right. Remember depth-first traversal?
Printing the rightmost edges. You would need to print from bottom to top. The key is printing in the opposite order. Easy
if you know how to manipulate recursion into a stack-based solution. Remember post-order traversal?

Solution:
We could divide the tree into two subtrees. One is the left subtree, and the other is the right subtree. For the left subtree, we print
the leftmost edges from top to bottom. Then we print its leaves from left to right. For the right subtree, we print its leaves from left
to right, then its rightmost edges from bottom to top.

Printing the leaves from left to right order is a classic example of depth-first traversal. If you are not familiar with depth-first
traversal, you should attempt other questions such as Maximum Height of a Binary Tree, Populating Next Right Pointers in Each
Node. How do you know if a node is a leaf? Easy, if the node does not have both left and right children.

Printing the leftmost edges is just a trivial addition to the depth-first traversal. When a node is a leftmost edge, then its left child
must also be a leftmost edge. We could use a variable to pass this information down the tree. Please note that you must add the
condition to avoid printing the node twice (a node could be a leftmost edge as well as a leaf node).

Printing the rightmost edges from bottom to top is easy if you realize the trick of manipulating recursion as a stack. Try relate this
with how post-order traversal works. When calling recursive functions, function calls are pushed onto a stack, and therefore you
could use the implicit stack to your advantage. This trick is especially handy when applied in situation where you need to reverse
the order of operations. See my post: Recursion to the Rescue! and Reversing Linked List Iteratively and Recursively for more
practice using this trick.

This solution works whether the tree itself is complete or not.

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 2/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

void printLeftEdges(BinaryTree *p, bool print) {
  if (!p) return;
  if (print || (!p­>left && !p­>right))
    cout << p­>data << " ";
  printLeftEdges(p­>left, print);
  printLeftEdges(p­>right, false);
}
 
void printRightEdges(BinaryTree *p, bool print) {
  if (!p) return;
  printRightEdges(p­>left, false);
  printRightEdges(p­>right, print);
  if (print || (!p­>left && !p­>right))
    cout << p­>data << " ";
}
 
void printOuterEdges(BinaryTree *root) {
  if (!root) return;
  cout << root­>data << " ";
  printLeftEdges(root­>left, true);
  printRightEdges(root­>right, true);
}

Further Thoughts:
Please note that the above problem statement does not give a clear definition of “left-most” node. Thanks to my dear readers
who pointed out this ambiguity, which I didn’t consider earlier in my above solution.

For example, consider the binary tree below: Is node 8 a left-most node?

   _______________28_______________
/ \
4____ ____69
\ /
__8__ __56__
/ \ / \
__7 12__ __34 __27__
/ \ / / \
5_ _13 _2 _3 39
\ / / /
6 11 10 9

The above code prints: 28, 4, 6, 11, 10, 9, 39, 69.

It seems that nodes 8, 7, and 5 should be left-most nodes as well, but are not printed by the above code. This is because we
made an implicit assumption that all left-most nodes could only be reached by following each node’s left branch (similar for right-
most nodes).

To remedy this, we use a modified recursive definition for left-most node (similar for right-most node):

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 3/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

If a node is a left-most node, then its left child must be a left-most node as well.
If its left child does not exist, then its right child will be a left-most node.

The below code prints the correct output of: 28, 4, 8, 7, 5, 6, 11, 10, 9, 39, 27, 56, 69.

All that are needed is just two lines of code changes.

void printLeftEdges(BinaryTree *p, bool print) {


if (!p) return;
if (print || (!p->left && !p->right))
cout << p->data << " ";
printLeftEdges(p->left, print);
printLeftEdges(p->right, (print && !p->left ? true : false));
}

void printRightEdges(BinaryTree *p, bool print) {


if (!p) return;
printRightEdges(p->left, (print && !p->right ? true : false));
printRightEdges(p->right, print);
if (print || (!p->left && !p->right))
cout << p->data << " ";
}

void printOuterEdges(BinaryTree *root) {


if (!root) return;
cout << root->data << " ";
printLeftEdges(root->left, true);
printRightEdges(root->right, true);
}

Note:
Some readers claimed that the above algorithm does not always print all edge nodes. For example, by moving node 5 to be the
left child of node 12, we get the binary tree below:

_______________28_______________
/ \
4___ ____69
\ /
___8__ __56__
/ \ / \
7 __12___ __34 __27__
/ \ / / \
5_ _13 _2 _3 39

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 4/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

\ / / /
6 11 10 9

And the algorithm in the Further Thoughts section prints the following:
28, 4, 8, 7, 6, 11, 10, 9, 39, 27, 56, 69.

Some readers argued that node 5 should be printed too. However, if you read the definition of “left-most node” carefully in the
above section, you would agree with me that node 5 is not a left-most node. This is because node 8 (which is a left-most node)
has a left child, node 7. Therefore, node 7 is a left-most node but not node 12. As a result, all descendants of node 12 must not
be left-most nodes.

You could use your own definition of left-most node as you pleased, but keep in mind that the problem statement is ambiguous to
start with, so communicating this with your interviewer will be your best bet.

Rating: 4.7/5 (27 votes cast)

Print Edge Nodes (Boundary) of a Binary Tree, 4.7 out of 5 based on 27 ratings

← Implement strstr() to Find a Substring in a String Binary Tree Post-Order Traversal Iterative Solution →

49 thoughts on “Print Edge Nodes (Boundary) of a Binary Tree”

toplanguage
October 21, 2010 at 5:38 pm

Good analysis! Thank you.

Reply ↓ 0

Anonymous
October 27, 2010 at 11:42 am

_30_
/\
10 20
/\
45 35

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 5/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

/\
42 47

Just wonder, in this binary tree, is 45 an edge node? It's not on the left most path, but it seems to look like an edge.

Reply ↓ 0

Claud
January 15, 2013 at 10:01 pm

I got a straightforward solution in C. Seems to handles all cases.

void hkTreeInorderTraversalLeavesOnly(hkTreeNode *root)
{
    if(!root)
        return;
 
    hkTreeInorderTraversalLeavesOnly(root­>left);
    if(!root­>left && !root­>right)
        printf("%d ", root­>key);
    hkTreeInorderTraversalLeavesOnly(root­>right);
}
 
static void hkTreePrintLeftEdge(hkTreeNode *root)
{
    if(!root)
        return;
 
    hkTreeNode *node = root­>left;
    hkTreeNode *parent = root;
    bool done = false;
    while (!done)
    {
        while(node)
        {
            if (!node­>left && !node­>right) //don't print the last leaf
            {
                done = true;
                break;
            }
            printf("%d ", node­>key);
            parent = node;
            node = node­>left;
        }
        if (parent­>right) // turns right
            node = parent­>right;
        else
            break;
    }
}
 
static void hkTreePrintRightEdge(hkTreeNode *root)
{
    if(!root)
        return;

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 6/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode
 
    hkTreeNode *node = root­>right;
    hkTreeNode *parent = root;
    bool done = false;
    hkStack *stack = hkStackNew();
    if (stack)
    {
        while (!done)
        {
            while(node)
            {
                if (!node­>left && !node­>right) //don't print the last leaf
                {
                    done = true;
                    break;
                }
                //printf﴾"%d ", node­>key﴿;
                hkStackPush(stack, node);
                parent = node;
                node = node­>right;
            }
            if (parent­>left) // turns left
                node = parent­>left;
            else
                break;
        }
 
        // Dump stack: pop and print
        while(hkStackCount(stack))
        {
            /* pop out one and visit it */
            node = (hkTreeNode *)hkStackPopRtn(stack);
            if(node)
            {
                printf("%d ", node­>key);
            }
            else /* stack is empty now */
                break;
        }
        hkStackDelete(stack);
    }
}
 
void hkTreePrintEdgesAnticlockwise(hkTreeNode *root)
{
    if(!root)
        return;
 
    printf("hkTreePrintEdgesAnticlockwise:\n");
    // print root
    printf("%d ", root­>key);
 
    // print left side
    hkTreePrintLeftEdge(root);
 
    //in­order­traversal: print only leaves
    hkTreeInorderTraversalLeavesOnly(root);
 
    // print right side in reverse order
    hkTreePrintRightEdge(root);
 
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 7/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode
    printf("\n");
}

Reply ↓ 0

1337c0d3r
October 27, 2010 at 12:29 pm

The formatting is kinda messed-up.


I assume you meant 45 is the left child of 20, and you want to know if 45 is an edge node, right?
Yeah it does look like an edge node, and this is a good question to ask during the interview. The question is not clear enought to
answer your question, unfortunately. However, it shouldn't be hard to modify the solution to adapt to the new requirements. You
can pass information down the nodes as you traverse the leftmost nodes. This requirement still stay the same, that is, the child of
a leftmost node must also be a leftmost node.

Reply ↓ 0

Chimpanzee
November 10, 2010 at 8:01 pm

Very decent algorithm. When I tried to attack this problem, I used sometime like.

void PrintEdge(Node* root)


{
PrintLeftEdge(root);
PrintLeaves(root);
PrintRightEdge(root->right);
}

PrintLeaves() is just DFS. So the complexity of my algorithm is overall O(n).

Reply ↓ 0

Chimpanzee
November 10, 2010 at 8:12 pm

By the way, I am also very interested in hearing different solutions for finding the least common ancestor of two nodes in a binary
tree.

There could be several variations for this problem.


(1) each node can have a parent pointer.
(2) If each node doesn't have a parent, how to solve it?

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 8/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

Topcoder has an article telling about an advanced algorithm to solve this problem. But I don't think it is doable for an interview
session.

This is a Google phone interview question for which I was asked.

Reply ↓ 0

1337c0d3r
November 10, 2010 at 8:19 pm

You raise an interesting point.


The LCA of two nodes in a BST is easy to solve. But for general binary tree it is non-trivial. Are you sure they asked this question
during an interview? Even so, I believe they do not expect something like in topcoder article. Maybe a brute force approach will
do?

Reply ↓ 0

Raj
May 8, 2011 at 7:51 pm

Yes. What about a BFS to find a path to the first node, and keeping the path in a queue. Now do a BFS to find a path to the
second node, and when done, see which nodes are common from the saved queue

Reply ↓ 0

hangyu
November 25, 2010 at 11:54 am

Hi, you code seems could not print out the edge of this tree.

28
/\
/\
/\
/\
/\
/\
4 69
\/\
8/\
/ \ 56 233
/\/
7 12 34
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_enU… 9/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

/\
5 13
\
6

Base on your code the output is 28 4 6 13 34 233 69

As node 8 is the right tree and also have two child, so your code does not print out its value.

Could you check?

Reply ↓ 0

hangyu
November 25, 2010 at 12:07 pm

Hi, the tree is messy, But can you try to create this tree and using your code. You will see.

int data[]={28,4,69,233,4,8,4,56,34,56,7,5,6,7,4,5,6,12,13};

To resolve this, it seems you need to modify your code to be this:

void printLeftEdges(BinaryTree *p, bool print) {


if (!p) return;
if (print || (!p->left && !p->right))
cout << p->data << " ";
printLeftEdges(p->left, (print ? true : false));
printLeftEdges(p->right, (p->left ? false: true);
}

Same for the print out of the right edge.

Reply ↓ 0

1337c0d3r
November 25, 2010 at 12:35 pm

>>As node 8 is the right tree and also have two child, so your code does not print out its value.

Are you sure node 8 is the rightmost node? That is, is it reachable by following the right child of each node from its root?

Could you specify which method you use to create the tree? Is the tree a BST or binary tree? If it is a BST, there are duplicate
values in it, how do you insert those nodes in? If it is a binary tree, then you have to specify the sentinel in it.

0
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 10/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode
0
Reply ↓

hangyu
November 26, 2010 at 11:30 am

Hi, I attached the tree img on mitbbs here:

http://www.mitbbs.com/article_t/test/31181365.html

Reply ↓ 0

1337c0d3r
November 26, 2010 at 3:31 pm

Thanks, hangyu.

Your modified solution is very close to being correct. It does print the correct output for your above example.

I changed your example a little (see my updated post above) to show a counter-example. You just need to add one extra
condition in your modified solution.

Reply ↓ 0

1337c0d3r
November 26, 2010 at 3:39 pm

@All:
Thanks to my kind readers who pointed out a problem with the above solution. See my update and the remedy in the "Further
Thoughts" section. Thanks

Reply ↓ 0

Tracy
January 25, 2011 at 8:29 pm

Why put "print ? true : false" instead of simply "print". Aren't they the same?…

Reply ↓ +1

1337c0d3r
January 25, 2011 at 10:34 pm

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 11/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

No. Not sure if you understand c++ syntax.


In C++, "printLeftEdges(p->left, (print ? true : false));" simply means the following:
if (print) {
printLeftEdges(p->left, true);
} else {
printLeftEdges(p->left, false);
}

Reply ↓ 0

Tracy
January 26, 2011 at 6:22 pm

@1337c0d3r:
I'm totally confused… you are actually passing variable print, which is of type bool, to function printLeftEdges(). Since you are
passing by value in C++ anyway,

printLeftEdges(p->left, print);

is not equal to the following?????

if (print) {
printLeftEdges(p->left, true);
} else {
printLeftEdges(p->left, false);
}

Reply ↓ 0

1337c0d3r
January 26, 2011 at 6:32 pm

@Tracy:
Haha, you're right! I am so embarrassed for this. I don't quite understand why I would do such thing, but I believe it must have
happened due to me refactoring the code.

Thanks to you for pointing this out. Above code is corrected.

Reply ↓ 0

airfang
March 4, 2011 at 9:54 pm

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 12/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

Quick question, what does “complete” binary tree mean in the problem statement? Is it referring to the “perfect” binary tree as
defined in the wikipedia, that the number of nodes = 2^(h+1) – 1?

Reply ↓ 0

1337c0d3r Post author

March 4, 2011 at 10:36 pm

My post is assuming that a complete binary tree is a perfect binary tree. But I believe the original question defines a complete
binary tree as:

A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far
left as possible.

Then according to the above definition, there would be no ambiguity which is pointed out in my post. Anyway, the general
solution above works whether the tree is complete or not.

Reply ↓ 0

xTristan
May 3, 2011 at 10:19 pm

in your examples, they are no longer complete binary trees so I assume you are trying to solve it for general binary tree cases.

In that case, for the updated definition in further thoughts, it is possible that a right-most node is in the left sub tree as well, if
there is no other nodes in the same level on its right. For example, in the images you have in Further thoughts, remove the
subtree starting from node 34 and node 27. In that case, node 12 becomes a right-most node by your updated definition. The
updated code would still not be able to print node 12.

More complicated, if you remove the node 56 and all its decedents, node 8 becomes both a left-most node and right-most node
by your update definition. Should it be printed twice such as 4->8->7->5->6->11->13->12->8->69?

Reply ↓ 0

xTristan
May 3, 2011 at 10:28 pm

an even simpler counter example, add a left node to node 4 in the image in “Further Thoughts” section. Your program will
not be able to print node 7 in that case.

After trying a few things, I think in that updated definition, the only way to work with the general case would be BFS,
with the help of a stack to hold on to the right-most nodes first before all leaf nodes are printed.

0
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 13/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode
Reply ↓

codingC
June 1, 2011 at 11:47 pm

Hi. There still is ambiguity in your updated solution. Say the root “r” has only one child, “lChild”, the left child . Suppose that the
node “lChild” has two children “ll” and “lr”. Should the edge lChild->lr be counted as one of the right-most edges? If the edge is
counted. Then how should the edge r–>lChild? left-most or right-most or both? print it out once or twice?

Reply ↓ 0

xyyang
July 15, 2011 at 7:03 pm

Your updated solution still does not work for some case, e.g. if you put node 5 as the left child of node 12, then your updated
solution can not print 5.
Here is my solution:
void printLeftEdges(Node * node, int cur_dep, int & max_dep)
{
if (!node)
return;
bool leftmost = false;
if (cur_dep>max_dep)
{
leftmost = true;
max_dep = cur_dep;
}
if (leftmost || (!node->left && !node->right))
cout<value<left, cur_dep+1, max_dep);
printLeftEdges(node->right, cur_dep+1, max_dep);
}

void printRightEdges(Node * node, int cur_dep, int & max_dep, stack & s)
{
if (!node)
return;
bool rightmost = false;
if (cur_dep>max_dep)
{
rightmost = true;
max_dep = cur_dep;
}
if (rightmost || (!node->left && !node->right))

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 14/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

s.push(node);
printRightEdges(node->right, cur_dep+1, max_dep,s);
printRightEdges(node->left, cur_dep+1, max_dep,s);
}

void printOuterEdges(Node * root)


{
if (!root)
return;
cout<data<left,1, left_depth);
cout<<" ";
stack s;
printRightEdges(root->right,1, right_depth, s);
while (!s.empty())
{
cout<data<<" ";
s.pop();
}
}

Reply ↓ 0

Sean
July 17, 2011 at 5:29 pm

Good observation!

Reply ↓ 0

1337c0d3r Post author

July 18, 2011 at 12:11 am

Thanks for pointing out this. I have updated my post with the “Note: section” at the end. According my own “left-most node”
definition, the algorithm in “Further thoughts” section is correct. You can use whatever definition of “left-most node” as you
pleased, since the problem statement is ambiguous to start with.

Reply ↓ 0

Pingback: Print Edge Nodes (Boundary) of a Binary Tree | 口├人人│의 Blog


Bala
August 2, 2011 at 6:38 am

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 15/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

@ihas1337code: A minor comment

The below code

printLeftEdges(p->right, (print && !p->left ? true : false)); –> line 6


printRightEdges(p->left, (print && !p->right ? true : false)); –> line 11

can be re-written as

printLeftEdges(p->right, !p->left ? print : false)); –> line 6


printRightEdges(p->left, !p->right ? print : false)); –> line 11

Reply ↓ 0

Pingback: C++ 面试题 C++笔试题 C++编程,数据结构,算法类面试题集(5) - IT公司面试手册


jarricochen
October 25, 2011 at 11:23 pm

It seems you still miss the case where the root has only right subtree. For example, if you remove the left subtree of the root
node of the tree in the Further Thought section, your final code will produce 28 10 9 39 27 56 69, but according to your final
definition, the correct answer should be 28 69 56 34 2 10 9 39 27 56 69. The get rid of this problem, you should whether the root
has only one subtree. If so, apply printOuterEdges() to that subtree.

Reply ↓ 0

Jon
December 1, 2011 at 1:49 pm

This is my solution (in a pythonic language), though I haven’t tested it yet…

def printBoundaries(Node n, leftMost, rightMost):


if n is None:
break
elif n.left is None and n.right is None:
print n
else:
if leftMost or rightMost:
print n
if leftMost and rightMost:
printBoundaries(n.left, true, n.right is None)
printBoundaries(n.right, false, n.left is None)
else:

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 16/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

printBoundaries(n.left, leftMost and not rightMost, rightMost and n.right is None)


printBoundaries(n.right, leftMost and n.left is None, rightMost and not leftMost)

printBoundaries(root, true, true)

Reply ↓ Report user 0

Jon
December 1, 2011 at 1:50 pm

Oops, identation lost

Reply ↓ Report user 0

Naman
March 13, 2012 at 3:10 am

6
/
3
/\
78
/
9

i think i this 8 will not print…

Reply ↓ +1

Naman
March 13, 2012 at 3:12 am

left child 9 is child of 8

Reply ↓ 0

Venki
April 12, 2012 at 10:50 pm

By “boundary” I would expect those nodes that can be seen if any one stands on left side, down side and right side of the tree. In
technical terms boundary includes left view, bottom view and right view of a binary tree.

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 17/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

The following code prints the boundary of a binary tree.

// Printing boundary nodes of tree
 
//   _______________28_______________
//  /                                \
//  4____                        ____69
//       \                      /
//      __8__                __56__
//     /     \              /      \
//   __7     12__        __34    __27__
//  /            \      /       /      \
//  5_          _13    _2      _3      39
//    \        /      /       /
//     6      11     10       9
 
#include 
using namespace std;
 
struct Node
{
    Node(int data) : key(data), lChild(NULL), rChild(NULL)
    {}
 
    int key;
    Node *lChild;
    Node *rChild;

 
Node *getNode(int key)
{
    return new Node(key);
}
 
bool isLeafNode(Node *pNode)
{
    return !pNode­>lChild && !pNode­>rChild;
}
 
void printLeftNodes(Node *root)
{
    if( root == NULL || isLeafNode(root) ) return;
 
 
    if( root­>lChild )
    {
        cout <key <lChild);
    }
    else
    {
        // if left NULL, right child is on out side
        cout <key <rChild);
    }
}
 
void printLeafNodes(Node *root)
{
    if( root == NULL ) return;
 
    if( isLeafNode(root) )
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 18/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode
    {
        cout <key <lChild);
    printLeafNodes(root­>rChild);
}
 
void printRightNodes(Node *root)
{
    if( root == NULL || isLeafNode(root) ) return;
 
    if( root­>rChild )
    {
        printRightNodes(root­>rChild);
        cout <key <lChild);
        cout <key << "\t";
    }
}
 
void printBoundaryNodes(Node *root)
{
    if( root )
    {
        cout <key <lChild);
        printLeafNodes(root);
        printRightNodes(root­>rChild);
        
        cout <lChild = getNode(4);
    root­>rChild = getNode(69);
 
    root­>lChild­>rChild = getNode(8);
    root­>rChild­>lChild = getNode(56);
 
    root­>lChild­>rChild­>lChild = getNode(7);
    root­>lChild­>rChild­>rChild = getNode(12);
 
    root­>rChild­>lChild­>lChild = getNode(34);
    root­>rChild­>lChild­>rChild = getNode(27);
 
    root­>lChild­>rChild­>lChild­>lChild = getNode(5);
    root­>lChild­>rChild­>rChild­>rChild = getNode(13);
 
    root­>lChild­>rChild­>lChild­>lChild­>rChild = getNode(6);
    root­>lChild­>rChild­>rChild­>rChild­>lChild = getNode(11);
 
    root­>rChild­>lChild­>lChild­>lChild = getNode(2);
    root­>rChild­>lChild­>lChild­>lChild­>lChild = getNode(10);
 
    root­>rChild­>lChild­>rChild­>lChild = getNode(3);
    root­>rChild­>lChild­>rChild­>lChild­>lChild = getNode(9);
 
    root­>rChild­>lChild­>rChild­>rChild = getNode(39);
 
    return root;
}
 
int main()
{
    printBoundaryNodes(bultTestTree_1());
    return 0;
}

0
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 19/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode
0
Reply ↓ Report user

Venki
April 12, 2012 at 10:52 pm

Some how the previous code was not fully printed, here is the code

#include 
using namespace std;
 
struct Node
{
    Node(int data) : key(data), lChild(NULL), rChild(NULL)
    {}
 
    int key;
    Node *lChild;
    Node *rChild;

 
Node *getNode(int key)
{
    return new Node(key);
}
 
bool isLeafNode(Node *pNode)
{
    return !pNode­>lChild && !pNode­>rChild;
}
 
void printLeftNodes(Node *root)
{
    if( root == NULL || isLeafNode(root) ) return;
 
 
    if( root­>lChild )
    {
        cout <key <lChild);
    }
    else
    {
        // if left NULL, right child is on out side
        cout <key <rChild);
    }
}
 
void printLeafNodes(Node *root)
{
    if( root == NULL ) return;
 
    if( isLeafNode(root) )
    {
        cout <key <lChild);
    printLeafNodes(root­>rChild);
}
 
void printRightNodes(Node *root)

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 20/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode
{
    if( root == NULL || isLeafNode(root) ) return;
 
    if( root­>rChild )
    {
        printRightNodes(root­>rChild);
        cout <key <lChild);
        cout <key << "\t";
    }
}
 
void printBoundaryNodes(Node *root)
{
    if( root )
    {
        cout <key <lChild);
        printLeafNodes(root);
        printRightNodes(root­>rChild);
        
        cout <lChild = getNode(4);
    root­>rChild = getNode(69);
 
    root­>lChild­>rChild = getNode(8);
    root­>rChild­>lChild = getNode(56);
 
    root­>lChild­>rChild­>lChild = getNode(7);
    root­>lChild­>rChild­>rChild = getNode(12);
 
    root­>rChild­>lChild­>lChild = getNode(34);
    root­>rChild­>lChild­>rChild = getNode(27);
 
    root­>lChild­>rChild­>lChild­>lChild = getNode(5);
    root­>lChild­>rChild­>rChild­>rChild = getNode(13);
 
    root­>lChild­>rChild­>lChild­>lChild­>rChild = getNode(6);
    root­>lChild­>rChild­>rChild­>rChild­>lChild = getNode(11);
 
    root­>rChild­>lChild­>lChild­>lChild = getNode(2);
    root­>rChild­>lChild­>lChild­>lChild­>lChild = getNode(10);
 
    root­>rChild­>lChild­>rChild­>lChild = getNode(3);
    root­>rChild­>lChild­>rChild­>lChild­>lChild = getNode(9);
 
    root­>rChild­>lChild­>rChild­>rChild = getNode(39);
 
    return root;
}
 
int main()
{
    printBoundaryNodes(bultTestTree_1());
    return 0;
}

Reply ↓ Report user 0

Balaji

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 21/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

September 16, 2012 at 8:15 pm

1337c0d3r – what about doing a BFS and print the first and last node in each level(Of course we can track if are moving into next
level while doing BFS – from your post ) And also print all the nodes of last level..?

Reply ↓ Report user 0

Kamal
September 23, 2012 at 2:24 am

Elaborate solution here: http://www.ritambhara.in/print-edge-nodes-boundary-nodes-of-a-binary-tree/

Reply ↓ 0

Pingback: Amazon interview preparation | What I learn, I blog !

Pingback: C++ 编程,数据结构,算法类面试题集(5) | Micro Blog


Butler
April 8, 2013 at 8:45 pm

How about this>

void printEdgeNodes(TreeNode * p, bool print, bool left){
    bool leaf = p­>left==null&&p­>right==null;
    if(leaf || print){
        cout<val;
    }
    printEdgeNodes(p­>left, print&&left, left);
    printEdgeNodes(p­>right, print&&!left, left);
}
void main(){
    cout<val;
    printEdgeNodes(root­>left, true, true);
    printEdgeNodes(root­>right, true, false);
}

Reply ↓ 0

purple
July 23, 2013 at 8:29 pm

My solution is:
(1)print root
(2)BFS the tree
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 22/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

(2.1) print the 1st node of each level (they are the left most nodes), and push the last node into a stack(they are the right most
nodes)
(2.2) print the every node of the last level
(3) pop from the stack and print (they are the right most nodes)

Reply ↓ Report user 0

Hardy
October 4, 2013 at 9:01 am

Just want to contribute my code.

 26     public static void printEdgeNodes(Node root) {
 27         if(root == null) {
 28             return ;
 29         }
 30         Node node = root;
 31         while(node.left != null || node.right != null) {
 32             System.out.print(node.value + " ");
 33             if(node.left != null) {
 34                 node = node.left;
 35             }
 36             else { //if﴾node.right != null﴿ {
 37                 node = node.right;
 38             }
 39         }
 40
 41         printLeaves(root);
 42
 43         Stack stack = new Stack();
 44         node = root.right;
 45         while(node.left != null || node.right != null) {
 46             stack.push(node);
 47             if(node.right != null) {
 48                 node = node.right;
 49             }
 50             else {
 51                 node = node.left;
 52             }
 53         }
 54
 55         while(!stack.isEmpty()) {
 56             System.out.print(stack.pop().value + " ");
 57         }
 58
 59         System.out.println();
 60     }

Reply ↓ Report user 0

theOtherWC
January 25, 2014 at 8:42 am

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 23/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

Wouldn’t it be sufficient (and perhaps a little bit clearer) to just define leftmost and rightmost to be the first and last node of a
certain level?

Reply ↓ 0

Leet
February 18, 2014 at 4:35 am

Basic idea is to use level order traversal.

public Iterable boundaryView(){
    Queue boundaryView = new Queue();
    Queue levelOrder = new Queue();
    if(root==null)  return boundaryView;
    TreeNode LS = new TreeNode();
    levelOrder.enqueue(root);   
    levelOrder.enqueue(LS);
    boundaryView.enqueue(root.data);
    boundaryView(levelOrder,LS, boundaryView);
   
    for(Data d: boundaryView)
      System.out.println(d);
    return boundaryView;
  }
 
  private void boundaryView(Queue levelOrder, TreeNode LS, Queue boundaryView){
    Stack rightView = new Stack();
    Queue leafs = new Queue();
   
    while(!levelOrder.isEmpty()){
      TreeNode current = levelOrder.dequeue();
      if(current.equals(LS)){
        if(!levelOrder.isEmpty() ){
          if(!isLeaf(levelOrder.peek()))
            boundaryView.enqueue(levelOrder.peek().data); 
          else  {
            boundaryView.enqueue(levelOrder.peek().data); 
            levelOrder.dequeue();
          }
          levelOrder.enqueue(LS);
        }
      }
      if(current!=LS && isLeaf(current) )
        leafs.enqueue(current.data);
     
      if( !levelOrder.isEmpty() && levelOrder.peek().equals(LS) )
        if( !isLeaf(current) )
          rightView.push(current.data);
        else{
         
        }
     
      if(current.left!=null)    levelOrder.enqueue(current.left);
      if(current.right!=null)   levelOrder.enqueue(current.right);
    }
   

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 24/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode
    while(!leafs.isEmpty())
      boundaryView.enqueue(leafs.dequeue());
   
    while(!rightView.isEmpty())
      boundaryView.enqueue(rightView.pop());
     
  }

Reply ↓ +1

Leet
February 18, 2014 at 4:36 am

I have tested for some test cases.


Plz correct me if this approach is wrong

Reply ↓ 0

Leet
February 18, 2014 at 4:41 am

Replace

if( !isLeaf(current) )
                    rightView.push(current.data);
                else{
                    
                }

with

if( !isLeaf(current) && current!=root)
                    rightView.push(current.data);
                else{
                    
                }

Reply ↓ 0

Raghu
June 18, 2016 at 9:41 am

What is the expected output for this example? a is root, c is a’s right child, d is c’s left child and e is d’s right child.
a
\
c
webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 25/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

/
d
\
e
Is the answer a, c, d, e or a,d,e,c?

Reply ↓ 0

Rajiv
August 22, 2016 at 5:49 am

a, e, d, c

Reply ↓ 0

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment

To embed your code, please use <code>your code here</code>.

You may use the <code> tag to embed your code.

Name *

Email *

Website

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 26/27
6/5/2019 Print Edge Nodes (Boundary) of a Binary Tree – LeetCode

CAPTCHA Code

Post Comment

Copyright © 2017 LeetCode  ·  Designed by BuddyBoss

       

webcache.googleusercontent.com/search?q=cache%3Ahttps%3A%2F%2Farticles.leetcode.com%2Fprint-edge-nodes-boundary-of-binary%2F&rlz=1C5CHFA_en… 27/27

Vous aimerez peut-être aussi