Data Structure & Algorithm: Level-1 Problem #48. Only-Child-Node

Given a binary tree, find out all the only-child-nodes in it. A node is considered only-child-node if it has no sibling. Print out the values for those nodes in ascending order.

Sample Input:

root = [1, 2, 3, 4, 5, None, 6, 7, None, None, 8, None, 9]

Sample Output:

[ 6, 7, 8, 9]

only child node

With the sample binary tree shown above, all the nodes marked with yellow are only-child-nodes.


数据结构和算法:初级练习题 #48 – 二叉树孤独节点


给定一颗二叉树,找出所有的孤独节点。如果一个节点只有一个子节点,那么这个子节点就是孤独节点。按照节点数值从小到大输出找到的结果。

输入样例:

root = [1, 2, 3, 4, 5, None, 6, 7, None, None, 8, None, 9]

输出样例:

[ 6, 7, 8, 9]


Python Solution

Only Child Nodes 3