返回介绍

solution / 0000-0099 / 0094.Binary Tree Inorder Traversal / README_EN

发布于 2024-06-17 01:04:05 字数 19128 浏览 0 评论 0 收藏 0

94. Binary Tree Inorder Traversal

中文文档

Description

Given the root of a binary tree, return _the inorder traversal of its nodes' values_.

 

Example 1:

Input: root = [1,null,2,3]
Output: [1,3,2]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [1]
Output: [1]

 

Constraints:

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

 

Follow up: Recursive solution is trivial, could you do it iteratively?

Solutions

Solution 1: Recursive Traversal

We first recursively traverse the left subtree, then visit the root node, and finally recursively traverse the right subtree.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree, and the space complexity mainly depends on the stack space of the recursive call.

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     self.val = val
#     self.left = left
#     self.right = right
class Solution:
  def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
    def dfs(root):
      if root is None:
        return
      dfs(root.left)
      ans.append(root.val)
      dfs(root.right)

    ans = []
    dfs(root)
    return ans
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode() {}
 *   TreeNode(int val) { this.val = val; }
 *   TreeNode(int val, TreeNode left, TreeNode right) {
 *     this.val = val;
 *     this.left = left;
 *     this.right = right;
 *   }
 * }
 */
class Solution {
  private List<Integer> ans = new ArrayList<>();

  public List<Integer> inorderTraversal(TreeNode root) {
    dfs(root);
    return ans;
  }

  private void dfs(TreeNode root) {
    if (root == null) {
      return;
    }
    dfs(root.left);
    ans.add(root.val);
    dfs(root.right);
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  vector<int> inorderTraversal(TreeNode* root) {
    vector<int> ans;
    function<void(TreeNode*)> dfs = [&](TreeNode* root) {
      if (!root) {
        return;
      }
      dfs(root->left);
      ans.push_back(root->val);
      dfs(root->right);
    };
    dfs(root);
    return ans;
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func inorderTraversal(root *TreeNode) (ans []int) {
  var dfs func(*TreeNode)
  dfs = func(root *TreeNode) {
    if root == nil {
      return
    }
    dfs(root.Left)
    ans = append(ans, root.Val)
    dfs(root.Right)
  }
  dfs(root)
  return
}
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *   val: number
 *   left: TreeNode | null
 *   right: TreeNode | null
 *   constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 *   }
 * }
 */

function inorderTraversal(root: TreeNode | null): number[] {
  const ans: number[] = [];
  const dfs = (root: TreeNode | null) => {
    if (!root) {
      return;
    }
    dfs(root.left);
    ans.push(root.val);
    dfs(root.right);
  };
  dfs(root);
  return ans;
}
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//   TreeNode {
//     val,
//     left: None,
//     right: None
//   }
//   }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
  fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, ans: &mut Vec<i32>) {
    if root.is_none() {
      return;
    }
    let node = root.as_ref().unwrap().borrow();
    Self::dfs(&node.left, ans);
    ans.push(node.val);
    Self::dfs(&node.right, ans);
  }

  pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
    let mut ans = vec![];
    Self::dfs(&root, &mut ans);
    ans
  }
}
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *   this.val = (val===undefined ? 0 : val)
 *   this.left = (left===undefined ? null : left)
 *   this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function (root) {
  const ans = [];
  const dfs = root => {
    if (!root) {
      return;
    }
    dfs(root.left);
    ans.push(root.val);
    dfs(root.right);
  };
  dfs(root);
  return ans;
};

Solution 2: Stack Implementation for Non-recursive Traversal

The non-recursive approach is as follows:

  1. Define a stack stk.
  2. Push the left nodes of the tree into the stack in sequence.
  3. When the left node is null, pop and process the top element of the stack.
  4. Repeat steps 2-3.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree, and the space complexity mainly depends on the stack space.

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     self.val = val
#     self.left = left
#     self.right = right
class Solution:
  def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
    ans, stk = [], []
    while root or stk:
      if root:
        stk.append(root)
        root = root.left
      else:
        root = stk.pop()
        ans.append(root.val)
        root = root.right
    return ans
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode() {}
 *   TreeNode(int val) { this.val = val; }
 *   TreeNode(int val, TreeNode left, TreeNode right) {
 *     this.val = val;
 *     this.left = left;
 *     this.right = right;
 *   }
 * }
 */
class Solution {
  public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> ans = new ArrayList<>();
    Deque<TreeNode> stk = new ArrayDeque<>();
    while (root != null || !stk.isEmpty()) {
      if (root != null) {
        stk.push(root);
        root = root.left;
      } else {
        root = stk.pop();
        ans.add(root.val);
        root = root.right;
      }
    }
    return ans;
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  vector<int> inorderTraversal(TreeNode* root) {
    vector<int> ans;
    stack<TreeNode*> stk;
    while (root || stk.size()) {
      if (root) {
        stk.push(root);
        root = root->left;
      } else {
        root = stk.top();
        stk.pop();
        ans.push_back(root->val);
        root = root->right;
      }
    }
    return ans;
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func inorderTraversal(root *TreeNode) (ans []int) {
  stk := []*TreeNode{}
  for root != nil || len(stk) > 0 {
    if root != nil {
      stk = append(stk, root)
      root = root.Left
    } else {
      root = stk[len(stk)-1]
      stk = stk[:len(stk)-1]
      ans = append(ans, root.Val)
      root = root.Right
    }
  }
  return
}
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *   val: number
 *   left: TreeNode | null
 *   right: TreeNode | null
 *   constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 *   }
 * }
 */

function inorderTraversal(root: TreeNode | null): number[] {
  const stk: TreeNode[] = [];
  const ans: number[] = [];
  while (root || stk.length > 0) {
    if (root) {
      stk.push(root);
      root = root.left;
    } else {
      root = stk.pop();
      ans.push(root.val);
      root = root.right;
    }
  }
  return ans;
}
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
//   pub val: i32,
//   pub left: Option<Rc<RefCell<TreeNode>>>,
//   pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
//   #[inline]
//   pub fn new(val: i32) -> Self {
//   TreeNode {
//     val,
//     left: None,
//     right: None
//   }
//   }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
  pub fn inorder_traversal(mut root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
    let mut ans = vec![];
    let mut stk = vec![];
    while root.is_some() || !stk.is_empty() {
      if root.is_some() {
        let next = root.as_mut().unwrap().borrow_mut().left.take();
        stk.push(root);
        root = next;
      } else {
        let mut node = stk.pop().unwrap();
        let mut node = node.as_mut().unwrap().borrow_mut();
        ans.push(node.val);
        root = node.right.take();
      }
    }
    ans
  }
}
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *   this.val = (val===undefined ? 0 : val)
 *   this.left = (left===undefined ? null : left)
 *   this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function (root) {
  const stk = [];
  const ans = [];
  while (root || stk.length > 0) {
    if (root) {
      stk.push(root);
      root = root.left;
    } else {
      root = stk.pop();
      ans.push(root.val);
      root = root.right;
    }
  }
  return ans;
};

Solution 3: Morris Implementation for In-order Traversal

Morris traversal does not require a stack, so the space complexity is $O(1)$. The core idea is:

Traverse the binary tree nodes,

  1. If the left subtree of the current node root is null, add the current node value to the result list ans, and update the current node to root.right.
  2. If the left subtree of the current node root is not null, find the rightmost node prev of the left subtree (which is the predecessor node of the root node in in-order traversal):
    • If the right subtree of the predecessor node prev is null, point the right subtree of the predecessor node to the current node root, and update the current node to root.left.
    • If the right subtree of the predecessor node prev is not null, add the current node value to the result list ans, then point the right subtree of the predecessor node to null (i.e., disconnect prev and root), and update the current node to root.right.
  3. Repeat the above steps until the binary tree node is null, and the traversal ends.

The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the number of nodes in the binary tree.

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     self.val = val
#     self.left = left
#     self.right = right
class Solution:
  def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
    ans = []
    while root:
      if root.left is None:
        ans.append(root.val)
        root = root.right
      else:
        prev = root.left
        while prev.right and prev.right != root:
          prev = prev.right
        if prev.right is None:
          prev.right = root
          root = root.left
        else:
          ans.append(root.val)
          prev.right = None
          root = root.right
    return ans
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode() {}
 *   TreeNode(int val) { this.val = val; }
 *   TreeNode(int val, TreeNode left, TreeNode right) {
 *     this.val = val;
 *     this.left = left;
 *     this.right = right;
 *   }
 * }
 */
class Solution {
  public List<Integer> inorderTraversal(TreeNode root) {
    List<Integer> ans = new ArrayList<>();
    while (root != null) {
      if (root.left == null) {
        ans.add(root.val);
        root = root.right;
      } else {
        TreeNode prev = root.left;
        while (prev.right != null && prev.right != root) {
          prev = prev.right;
        }
        if (prev.right == null) {
          prev.right = root;
          root = root.left;
        } else {
          ans.add(root.val);
          prev.right = null;
          root = root.right;
        }
      }
    }
    return ans;
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  vector<int> inorderTraversal(TreeNode* root) {
    vector<int> ans;
    while (root) {
      if (!root->left) {
        ans.push_back(root->val);
        root = root->right;
      } else {
        TreeNode* prev = root->left;
        while (prev->right && prev->right != root) {
          prev = prev->right;
        }
        if (!prev->right) {
          prev->right = root;
          root = root->left;
        } else {
          ans.push_back(root->val);
          prev->right = nullptr;
          root = root->right;
        }
      }
    }
    return ans;
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func inorderTraversal(root *TreeNode) (ans []int) {
  for root != nil {
    if root.Left == nil {
      ans = append(ans, root.Val)
      root = root.Right
    } else {
      prev := root.Left
      for prev.Right != nil && prev.Right != root {
        prev = prev.Right
      }
      if prev.Right == nil {
        prev.Right = root
        root = root.Left
      } else {
        ans = append(ans, root.Val)
        prev.Right = nil
        root = root.Right
      }
    }
  }
  return
}
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *   val: number
 *   left: TreeNode | null
 *   right: TreeNode | null
 *   constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 *   }
 * }
 */

function inorderTraversal(root: TreeNode | null): number[] {
  const ans: number[] = [];
  while (root) {
    if (!root.left) {
      ans.push(root.val);
      root = root.right;
    } else {
      let prev = root.left;
      while (prev.right && prev.right != root) {
        prev = prev.right;
      }
      if (!prev.right) {
        prev.right = root;
        root = root.left;
      } else {
        ans.push(root.val);
        prev.right = null;
        root = root.right;
      }
    }
  }
  return ans;
}
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *   this.val = (val===undefined ? 0 : val)
 *   this.left = (left===undefined ? null : left)
 *   this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var inorderTraversal = function (root) {
  const ans = [];
  while (root) {
    if (!root.left) {
      ans.push(root.val);
      root = root.right;
    } else {
      let prev = root.left;
      while (prev.right && prev.right != root) {
        prev = prev.right;
      }
      if (!prev.right) {
        prev.right = root;
        root = root.left;
      } else {
        ans.push(root.val);
        prev.right = null;
        root = root.right;
      }
    }
  }
  return ans;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文