FlattenTree

xiaoxiao2021-02-28  39

有一颗二叉树,将左子树的所有节点,转移到右子树上,如:

      1             1      /  \            \   2    3            2                      \                         3

代码:

/// <summary> /// 将root 中右子树->左子树,再将左子树->右子树 /// </summary> /// <param name="root"></param> public static void FlattenTree(TreeNode root) { if (root == null) return; FlattenTree(root.Left); FlattenTree(root.Right); if (root.Left == null) return; TreeNode node = root.Left; while (node.Right != null) node = node.Right; node.Right = root.Right; root.Right = root.Left; root.Left = null; }

转载请注明原文地址: https://www.6miu.com/read-1600375.html

最新回复(0)