Given a binary tree, flatten it to a linked list in-place.

For example,

Given

1 / \ 2 5 / \ \ 3 4 6

The flattened tree should look like:

1 \ 2 \ 3 \ 4 \ 5 \ 6

Skip to content
# Category: Depth First Search

Binary Tree · Depth First Search · DFS · leetcode · Trees## Flatten Binary Tree to Linked List

Binary Tree · Depth First Search · DFS · leetcode · Trees## Sum Root to Leaf Numbers

Binary Tree · Depth First Search · DFS · leetcode · Trees## Binary Tree Paths

Binary Tree · Depth First Search · DFS · leetcode · Trees## Path Sum II

Binary Tree · Depth First Search · DFS · leetcode · Trees## Path Sum

Binary Tree · Depth First Search · DFS · leetcode · Trees## Lowest Common Ancestor of a Binary Search Tree

Binary Tree · Depth First Search · DFS · leetcode · Trees## Lowest Common Ancestor of a Binary Tree

Binary Tree · Depth First Search · DFS · leetcode · Trees## Minimum Depth of Binary Tree

Binary Tree · Depth First Search · DFS · leetcode · Trees## Maximum Depth of Binary Tree

Binary Tree · Depth First Search · DFS · leetcode · Stack · Traversals · Trees## Binary Tree Postorder Traversal

Given a binary tree, flatten it to a linked list in-place.

For example,

Given

1 / \ 2 5 / \ \ 3 4 6

The flattened tree should look like:

1 \ 2 \ 3 \ 4 \ 5 \ 6

Given a binary tree containing digits from `0-9`

only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path `1->2->3`

which represents the number `123`

.

Find the total sum of all root-to-leaf numbers.

For example,

1 / \ 2 3

The root-to-leaf path `1->2`

represents the number `12`

.

The root-to-leaf path `1->3`

represents the number `13`

.

Return the sum = 12 + 13 = `25`

.

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

1 / \ 2 3 \ 5

All root-to-leaf paths are:

["1->2->5", "1->3"]

Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.

For example:

Given the below binary tree and `sum = 22`

,

5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1

return

[ [5,4,11,2], [5,8,4,5] ]

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:

Given the below binary tree and `sum = 22`

,

5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1

return true, as there exist a root-to-leaf path `5->4->11->2`

which sum is 22.

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow **a node to be a descendant of itself**).”

_______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5

For example, the lowest common ancestor (LCA) of nodes `2`

and `8`

is `6`

. Another example is LCA of nodes `2`

and `4`

is `2`

, since a node can be a descendant of itself according to the LCA definition.

Continue reading “Lowest Common Ancestor of a Binary Search Tree”

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow **a node to be a descendant of itself**).”

_______3______ / \ ___5__ ___1__ / \ / \ 6 _2 0 8 / \ 7 4

For example, the lowest common ancestor (LCA) of nodes `5`

and `1`

is `3`

. Another example is LCA of nodes `5`

and `4`

is `5`

, since a node can be a descendant of itself according to the LCA definition.

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Given a binary tree, return the *postorder* traversal of its nodes’ values.

For example:

Given binary tree `{1,#,2,3}`

,

1 \ 2 / 3

return `[3,2,1]`

.

**Note:** Recursive solution is trivial, could you do it iteratively?