Mirror image of a binary tree

前端 未结 13 2553
时光取名叫无心
时光取名叫无心 2020-12-01 09:00

Suppose there is a tree:

             1
            / \\
           2   3
              / \\
             4   5

Then the mirror image will

13条回答
  •  难免孤独
    2020-12-01 09:07

    void swap_node(node n) {
      if(n != null) {
        node tmp = n.left;
        n.left = n.right;
        n.right = tmp;
    
        swap_node(n.left);
        swap_node(n.right);
      }
    }
    
    swap_node(root);
    

提交回复
热议问题