LeetCode(sum-root-to-leaf-numbers)

好久没写博客了,写篇博客来记录以下自己找工作过程中刷的算法题吧!

题目描述

给定一个仅包含数字0-9的二叉树,每一条从根节点到叶子节点的路径都可以用一个数字表示。

例如根节点到叶子节点的一条路径是1->2->3,那么这条路径就用123来代替。

找出根节点到叶子节点的所有路径表示的数字之和

例如:

    1↵   / ↵  2   3

根节点到叶子节点的路径1->2用数字12代替

根节点到叶子节点的路径1->3用数字13代替

所以答案为12+13=25

 

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 path1->2->3which represents the number123.
Find the total sum of all root-to-leaf numbers.
For example,

    1↵   / ↵  2   3

The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.
Return the sum = 12 + 13 =25.

 

我的解决方案:

/**
 * 决定动脑子
 * 首先将四边上的O标记为*然后再深度遍历,将和它直接或间接相连的都改成*
 * 这些*就是指没有被完全包围的
 *
 */
public class LeetCode_21_2 {

    public void solve(char[][] board) {
        int height = board.length;
        int width = 0;
        if(height!=0)
            width = board[0].length;
        int i=0,j=0;
        while (j<width)
            digui(i,j++,width,height,board);//深度扫描
        j--;
        while (i<height)
            digui(i++,j,width,height,board);//深度扫描
        i--;
        while (j>=0)
            digui(i,j--,width,height,board);//深度扫描
        j++;
        while (i>=0)
            digui(i--,j,width,height,board);//深度扫描
        i++;
        //将剩余的O变为X
        for(i=0;i<height;i++) {
            for (j=0; j < width; j++) {
                if(board[i][j]=='O')
                    board[i][j]='X';
            }
        }
        //将剩余的*变回O
        for(i=0;i<height;i++) {
            for (j=0; j < width; j++) {
                if(board[i][j]=='*')
                    board[i][j]='O';
            }
        }
    }

    private void digui(int i,int j,int width,int height,char[][] board){
        if(board[i][j]=='O') {
            board[i][j] = '*';
            if (i > 0)
                digui(i - 1, j, width, height, board);
            if (i < (height-1))
                digui(i + 1, j, width, height, board);
            if (j > 0)
                digui(i, j - 1, width, height, board);
            if (j < (width-1))
                digui(i, j + 1, width, height, board);
        }
    }

    public static void main(String[] args) {
        LeetCode_21_2 leetCode_21_2 = new LeetCode_21_2();
        char[][] board = {
                {'X','X','X'},
                {'X','O','X'},
                {'X','X','X'}
        };
        leetCode_21_2.solve(board);
        int height = board.length;
        int width = 0;
        if(height>0)
            width = board[0].length;
        for(int i=0;i<height;i++) {
            for (int j = 0; j < width; j++) {
                System.out.print(board[i][j]+" ");
            }
            System.out.println("");
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值